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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| var HelloWorldLayer = cc.Layer.extend({ sprite:null, direction:"right", npcX:100, npcY:100, isStand:true, ctor:function () { this._super();
var size = cc.winSize; var punch = new cc.MenuItemFont("打",this.punchCall,this); punch.setPosition(size.width-100,100); var kick = new cc.MenuItemFont("踢",this.kickCall,this); kick.setPosition(size.width-100,150); var menu=new cc.Menu(punch,kick); menu.setPosition(0,0); this.addChild(menu); ccs.armatureDataManager.addArmatureFileInfo(res.stand_json); var stand=new ccs.Armature("stand"); stand.getAnimation().playWithIndex(0); stand.setPosition(this.npcX,this.npcY); this.addChild(stand); stand.setTag(1); return true; }, punchCall:function(){ this.npcX = this.getChildByTag(1).getPositionX(); this.removeChildByTag(1); ccs.armatureDataManager.addArmatureFileInfo(res.punch_json); var punch=new ccs.Armature("punch"); punch.getAnimation().playWithIndex(0); punch.setPosition(this.npcX,this.npcY); this.addChild(punch); punch.setTag(1); this.isStand = false; punch.getAnimation().setMovementEventCallFunc(this.animationEvent,this); }, kickCall:function(){ this.npcX = this.getChildByTag(1).getPositionX(); this.removeChildByTag(1); ccs.armatureDataManager.addArmatureFileInfo(res.kick_json); var kick=new ccs.Armature("kick"); kick.getAnimation().playWithIndex(0); kick.setPosition(this.npcX,this.npcY); this.addChild(kick); kick.setTag(1); this.isStand = false; kick.getAnimation().setMovementEventCallFunc(this.animationEvent,this); }, animationEvent:function (armature, movementType, movementID) { if(!this.isStand){ this.removeChildByTag(1); ccs.armatureDataManager.addArmatureFileInfo(res.stand_json); var stand=new ccs.Armature("stand"); stand.getAnimation().playWithIndex(0); stand.setPosition(this.npcX,this.npcY); this.addChild(stand); stand.setTag(1); this.isStand = 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); return true; }, touchbegan:function(touch,event){ event.getCurrentTarget().npcX = event.getCurrentTarget().getChildByTag(1).getPositionX(); event.getCurrentTarget().removeChildByTag(1); ccs.armatureDataManager.addArmatureFileInfo(res.walk_json); var walk=new ccs.Armature("walk"); walk.getAnimation().playWithIndex(0); walk.setPosition(event.getCurrentTarget().npcX,event.getCurrentTarget().npcY); event.getCurrentTarget().addChild(walk); walk.setTag(1); var x = touch.getLocation().x; var y = event.getCurrentTarget().npcY; var bn = event.getCurrentTarget().getChildByTag(1); bn.stopAllActions(); var time = Math.round(Math.sqrt(Math.pow(x-bn.getPositionX(),2)+Math.pow(y-bn.getPositionY(),2)))/100; bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc(function(bn){ var layer = bn.parent; bn.removeFromParent(); ccs.armatureDataManager.addArmatureFileInfo(res.stand_json); var stand=new ccs.Armature("stand"); stand.getAnimation().playWithIndex(0); stand.setPosition(bn.getPositionX(),bn.getPositionY()); layer.npcX = bn.getPositionX(); layer.npcY = bn.getPositionY(); layer.addChild(stand); stand.setTag(1); layer.isStand = true; },bn))); return true; }, touchmoved:function(){ }, touchended:function(){ } });
var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });
|