update中不能添加动画吗?请教一个问题

请教,为什么在onLoad可以播放的动画在update里卡住了?
var distance=0;
var ani;
cc.Class({
extends: cc.Component,

properties: {
  player:cc.Node,
},
onLoad () {
    var player=this.player.getPosition();
    var self=this.node.getPosition();
    this.distance=cc.pDistance(player,self);
    this.ani= this.node.getComponent(cc.Animation);
    //this.ani.play('nAttack');        
},

update (dt) {    
    if(this.distance>175){
        console.log(this.distance+'>175');
        this.ani.play('nRun');
    }
    if(this.distance<175){
        //console.log(this.distance+'<175');
        this.ani.play('nAttack');
    }
},

});

我滴乖乖,能播放出来就怪了

update每帧调用,也就是你的判断条件成功的话,他会一只调用play,你这么写逻辑肯定不行

唉,我刚接触creator,新手啊,请问有这块的demo吗,万分感谢!

有此方面demo的大神帮忙提供下了。万分谢谢!!

多看看文档吧,先了解下update是个什么样的函数,你不是有输出log吗?难道不会疯狂输出log吗?多思考一下

:grinning:问题解决了,粘贴上来,希望能帮助需要的人
onLoad() {
this.ani = this.node.getComponent(cc.Animation);
condition = false;
},
update(dt) {
var player = this.player.getPosition();
var self = this.node.getPosition();
this.distance = cc.pDistance(player, self);
if (this.distance <= 175) {
if (!condition) {
this.ani.play(‘nAttack’);
condition = true; //虽然一直满足执行条件,被调用的方法也只能执行一次;
}
}
else{
if (condition) {
this.ani.play(‘nRun’);
condition = false; //虽然一直满足执行条件,被调用的方法也只能执行一次;
}
}
},

所有能不放在update里的东西都不要放update里 对于游戏来说一帧有多么重要吗

我们做的消除游戏,当一定时间没操作给提示也是这么做的,不过总觉得这样做不靠谱,虽然也没找到更好的办法