var Sen = cc.Sprite.extend({ direction:1, direc:1, const:5, p:3, ctor:function(){ this._super("res/png_1.png"); this.const = parseInt(Math.random()*5) this.setPosition(Math.random()*cc.winSize.width,Math.random()*cc.winSize.height) this.scheduleUpdate() this._init() }, update:function(){ this.x += this.const*this.direction this.y += this.const*this.direc if(this.x < 0 || this.x >cc.winSize.width){ this.direction *= -1 } if(this.y < 0 || this.y >cc.winSize.height){ this.direc *= -1 } },
//点击事件 _init:function(){ var that = this; var listener = cc.EventListener.create({ event:cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches:true, onTouchBegan:function(touch, event){ var target = event.getCurrentTarget(); //取得持有侦听事件的对象 var locationInNode = target.convertToNodeSpace(touch.getLocation()); var s = target.getContentSize(); var rect = cc.rect(0,0, s.width, s.height); if(cc.rectContainsPoint(rect, locationInNode)){ that._replace(target) } } }); cc.eventManager.addListener(listener, this); }, _replace:function(target){ this.p-- if(this.p<=0){ target.removeFromParent(true) } this.initWithFile("res/png_"+this.p+".png") } })
var HelloWorldLayer = cc.Layer.extend({ ctor:function () { this._super(); this._init(); }, _init:function (){ for(var i=0;i<5;i++){ var sq = new Sen() this.addChild(sq) } } }); var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });