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
| var HelloWorldLayer = cc.Layer.extend({ sprite:null, npcDirect:"right", ctor:function () { this._super(); var bgTexture=cc.textureCache.addImage("res/bg.jpg"); var bnTexture=cc.textureCache.addImage("res/h2.png"); var bg=new cc.Sprite(bgTexture); this.addChild(bg); bg.setPosition(cc.winSize.width/2,cc.winSize.height/2); var bn=new cc.Sprite(bnTexture); this.addChild(bn); bn.setPosition(200,200); bn.setScale(0.1); bn.setTag(1); return true; }, onEnter:function(){ 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; var jumps = Math.round(Math.sqrt(Math.pow(x-bn.getPositionX(),2)+Math.pow(y-bn.getPositionY(),2)))/20; 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)); } bn.runAction(cc.jumpTo(time,cc.p(x,y),10,jumps)); return true; }, touchmoved:function(){ return true; }, touchended:function(touch,event){ return true; } }); var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });
|