1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| var HelloWorldLayer = cc.Layer.extend({ sprite:null, ac:null, npcDirect:"right", ctor:function () { this._super();
var size = cc.winSize; var bg=new cc.Sprite(res.bg_jpg); this.addChild(bg); bg.setPosition(size.width/2,size.height/2); var sphero=new cc.Sprite("res/walk01.png"); var animation=new cc.Animation(); for(var i=1;i<=5;i++) { var frameName="res/walk0"+i+".png"; animation.addSpriteFrameWithFile(frameName); } animation.setDelayPerUnit(0.1); this.ac=cc.animate(animation).repeatForever(); this.addChild(sphero); sphero.setTag(1); sphero.setPosition(200,200); return true; }, onEnter:function(touch,event){ this._super(); cc.eventManager.addListener({ event:cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches:true, onTouchBegan:this.touchbegan, onTouchMoved:this.touchmoved, onTouchEnded:this.touchended },this); }, touchbegan:function(touch,event){ var x = touch.getLocation().x; var y = touch.getLocation().y; var bn = event.getCurrentTarget().getChildByTag(1); var time = Math.round(Math.sqrt(Math.pow(x-bn.getPositionX(),2)+Math.pow(y-bn.getPositionY(),2)))/100; bn.stopAllActions(); if(event.getCurrentTarget().npcDirect=="right"&&x<bn.getPositionX()){ event.getCurrentTarget().npcDirect = "left"; bn.runAction(cc.flipX(true)); }else if(event.getCurrentTarget().npcDirect=="left"&&x>bn.getPositionX()){ event.getCurrentTarget().npcDirect = "right"; bn.runAction(cc.flipX(false)); } cc.log(event.getCurrentTarget().ac); bn.runAction(event.getCurrentTarget().ac); bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc(function(bn){ bn.stopAllActions(); bn.setTexture("res/walk01.png"); },bn))); return true; }, touchmoved:function(touch,event){ }, touchended:function(touch,event){
} });
var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });
|