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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| var HelloWorldLayer = cc.Layer.extend({ sprite:null, chooseRole:0, ctor:function () { this._super(); var bg=new cc.Sprite(res.bg_jpg); this.addChild(bg); bg.setPosition(cc.winSize.width/2,cc.winSize.height/2); sp1Texture=cc.textureCache.addImage("res/1.png"); sp2Texture=cc.textureCache.addImage("res/2.png"); var sp1FrameUp=[]; var sp1FrameDown=[]; var sp1FrameLeft=[]; var sp1FrameRight=[]; var sp2FrameUp=[]; var sp2FrameDown=[]; var sp2FrameLeft=[]; var sp2FrameRight=[]; for(var n=0;n<4;n++) { var sp1Nowframedown=new cc.SpriteFrame(sp1Texture,cc.rect(32*n,0,32,48)); var sp1Nowframeleft=new cc.SpriteFrame(sp1Texture,cc.rect(32*n,48,32,48)); var sp1Nowframeright=new cc.SpriteFrame(sp1Texture,cc.rect(32*n,48*2,32,48)); var sp1Nowframeup=new cc.SpriteFrame(sp1Texture,cc.rect(32*n,48*3,32,48)); sp1FrameDown.push(sp1Nowframedown); sp1FrameLeft.push(sp1Nowframeleft); sp1FrameRight.push(sp1Nowframeright); sp1FrameUp.push(sp1Nowframeup); var sp2Nowframedown=new cc.SpriteFrame(sp2Texture,cc.rect(32*n,0,32,48)); var sp2Nowframeleft=new cc.SpriteFrame(sp2Texture,cc.rect(32*n,48*1,32,48)); var sp2Nowframeright=new cc.SpriteFrame(sp2Texture,cc.rect(32*n,48*2,32,48)); var sp2Nowframeup=new cc.SpriteFrame(sp2Texture,cc.rect(32*n,48*3,32,48)); sp2FrameUp.push(sp2Nowframeup); sp2FrameDown.push(sp2Nowframedown); sp2FrameLeft.push(sp2Nowframeleft); sp2FrameRight.push(sp2Nowframeright); } var sp1AnimationUp=new cc.Animation(sp1FrameUp,0.1); var sp1AnimationDown=new cc.Animation(sp1FrameDown,0.1); var sp1AnimationLeft=new cc.Animation(sp1FrameLeft,0.1); var sp1AnimationRight=new cc.Animation(sp1FrameRight,0.1); var sp2AnimationUp=new cc.Animation(sp2FrameUp,0.1); var sp2AnimationDown=new cc.Animation(sp2FrameDown,0.1); var sp2AnimationLeft=new cc.Animation(sp2FrameLeft,0.1); var sp2AnimationRight=new cc.Animation(sp2FrameRight,0.1); cc.animationCache.addAnimation(sp1AnimationUp,"sp1up"); cc.animationCache.addAnimation(sp1AnimationDown,"sp1down"); cc.animationCache.addAnimation(sp1AnimationLeft,"sp1left"); cc.animationCache.addAnimation(sp1AnimationRight,"sp1right"); cc.animationCache.addAnimation(sp2AnimationUp,"sp2up"); cc.animationCache.addAnimation(sp2AnimationDown,"sp2down"); cc.animationCache.addAnimation(sp2AnimationLeft,"sp2left"); cc.animationCache.addAnimation(sp2AnimationRight,"sp2right"); this.sprite=new cc.Sprite(sp1Texture,cc.rect(32,0,32,48)); this.chooseRole=1; this.addChild(this.sprite); this.sprite.setTag(1); this.sprite.setScale(4); this.sprite.setPosition(200,200); var sp1=new cc.MenuItemFont("选鸣人",this.callback,this); var sp2=new cc.MenuItemFont("选小樱",this.callback,this); sp1.setTag(11); sp2.setTag(12); sp1.setPositionY(50); var menu=new cc.Menu(sp1,sp2); this.addChild(menu); return true; }, getRole:function(){ return this.chooseRole; }, callback:function(obj){ switch(obj.tag){ case 11: this.sprite.stopAllActions(); this.sprite.setTexture(cc.textureCache.addImage("res/1.png"),cc.rect(32,0,32,48)); this.chooseRole=1; break; case 12: this.sprite.stopAllActions(); this.sprite.setTexture(cc.textureCache.addImage("res/2.png"),cc.rect(32,0,32,48)); this.chooseRole=2; break; } }, 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; bn.stopAllActions(); if(x<bn.getPositionX()){ if(Math.abs(x-bn.getPositionX())/Math.abs(y-bn.getPositionY())<1){ if(y<bn.getPositionY()){ var role = Number(event.getCurrentTarget().getRole()); var act=role==1?cc.animate(cc.animationCache.getAnimation("sp1down")).repeatForever():cc.animate(cc.animationCache.getAnimation("sp2down")).repeatForever(); bn.runAction(act); bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc( function(sprite){ sprite.stopAllActions(); },event.getCurrentTarget().sprite))); }else{ var role = event.getCurrentTarget().getRole(); var act=role==1?cc.animate(cc.animationCache.getAnimation("sp1up")).repeatForever():cc.animate(cc.animationCache.getAnimation("sp2up")).repeatForever(); bn.runAction(act); bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc( function(sprite){ sprite.stopAllActions(); },event.getCurrentTarget().sprite))); } } else if(Math.abs(x-bn.getPositionX())/Math.abs(y-bn.getPositionY())>=1){ var role = event.getCurrentTarget().getRole(); var act=role==1?cc.animate(cc.animationCache.getAnimation("sp1left")).repeatForever():cc.animate(cc.animationCache.getAnimation("sp2left")).repeatForever(); bn.runAction(act); bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc( function(sprite){ sprite.stopAllActions(); },event.getCurrentTarget().sprite))); } }else if(x>bn.getPositionX()){ if(Math.abs(x-bn.getPositionX())/Math.abs(y-bn.getPositionY())<1){ if(y<bn.getPositionY()){ var role = event.getCurrentTarget().getRole(); var act=role==1?cc.animate(cc.animationCache.getAnimation("sp1down")).repeatForever():cc.animate(cc.animationCache.getAnimation("sp2down")).repeatForever(); bn.runAction(act); bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc( function(sprite){ sprite.stopAllActions(); },event.getCurrentTarget().sprite))); }else{ var role = event.getCurrentTarget().getRole(); var act=role==1?cc.animate(cc.animationCache.getAnimation("sp1up")).repeatForever():cc.animate(cc.animationCache.getAnimation("sp2up")).repeatForever(); bn.runAction(act); bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc( function(sprite){ sprite.stopAllActions(); },event.getCurrentTarget().sprite))); } } else if(Math.abs(x-bn.getPositionX())/Math.abs(y-bn.getPositionY())>=1){ var role = event.getCurrentTarget().getRole(); var act=role==1?cc.animate(cc.animationCache.getAnimation("sp1right")).repeatForever():cc.animate(cc.animationCache.getAnimation("sp2right")).repeatForever(); bn.runAction(act); bn.runAction(cc.sequence(cc.moveTo(time,cc.p(x,y)),cc.callFunc( function(sprite){ sprite.stopAllActions(); },event.getCurrentTarget().sprite))); } } 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); } });
|