axmol/testjs/Javascript/test-nodes.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2012-04-18 03:43:08 +08:00
var director = cocos.Director.sharedDirector();
var scene = new cocos.Scene();
scene.init();
// our test sprite
var sprite = new cocos.Sprite();
sprite.initWithFile("Icon.png");
var pt = new cocos.Point();
2012-04-27 02:04:11 +08:00
pt.x = 160;
pt.y = 240;
2012-04-18 03:43:08 +08:00
sprite.position = pt;
// will call "update" every frame if there's an update property
2012-04-27 02:04:11 +08:00
//sprite.scheduleUpdate();
2012-04-18 03:43:08 +08:00
sprite.speed = new cocos.Point();
sprite.speed.x = Math.random() * 10 - 5;
sprite.speed.y = Math.random() * 10 - 5;
// the actual update function
sprite.update = function () {
var curPos = this.position;
curPos.x += this.speed.x;
curPos.y += this.speed.y;
this.position = curPos;
2012-04-27 02:04:11 +08:00
if (curPos.x >= 320 || curPos.x <= 0) this.speed.x *= -1;
if (curPos.y >= 480 || curPos.y <= 0) this.speed.y *= -1;
2012-04-18 03:43:08 +08:00
};
// adds the sprite to the scene
scene.addChild(sprite);
2012-04-27 02:04:11 +08:00
// add a menu
var item = new cocos.MenuItemImage();//Sprite();
//var normal = new cocos.Sprite(); normal.initWithFile("CloseNormal.png");
//var selected = new cocos.Sprite(); selected.initWithFile("CloseSelected.png");
//item.initWithNormalSprite(normal, selected);
2012-04-27 02:04:11 +08:00
item.initFromNormalImage("CloseNormal.png", "CloseSelected.png");
item.action = function () {
cocos.log("action");
};
cocos.log("normal image: " + item.normalImage);
pt.x = 20;
pt.y = 20;
item.position = pt;
var menu = new cocos.Menu();
menu.init();
menu.addChild(item);
pt.x = 0;
pt.y = 0;
menu.position = pt;
scene.addChild(menu, 1);
2012-04-18 03:43:08 +08:00
// let the games begin
director.runWithScene(scene);