我自己做了个小人,想用键盘控制他上下左右各个方向做走动动画。
我的构想是按着方向键的时候循环播放动画,放掉按键的时候人物停止。
但是为什么我按着方向键不放的时候过几秒这动画会一直保持在走动动画的第一帧不动了?
请教各位大侠有什么办法可解。为何那个教程里自带的羊没有这个问题。。
附上代码
cc.Class({
extends: cc.Component,
properties: {
hero:{
default: null,
type: cc.Node,
},
},
// use this for initialization
onLoad: function () {
this.setinput();
},
setinput : function(){
var self = this;
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event) {
switch(keyCode) {
case cc.KEY.a:
dir = 4;
self.hero.getComponent('myHero').changeDirection(dir);
break;
case cc.KEY.d:
dir = 6;
self.hero.getComponent('myHero').changeDirection(dir);
break;
}
},
onKeyReleased: function(keyCode, event) {
switch(keyCode) {
case cc.KEY.a:
dir = 40 ;
self.hero.getComponent('myHero').changeDirection(dir);
break;
}
},
},self.node);
},
}
```
cc.Class({
extends: cc.Component,
properties: {
AnimName :'',
},
changeDirection: function(dir){
this.getComponent(cc.Animation).play(this.AnimName + dir)
},
onLoad: function () {
},
});
```