我已经使用了setTimeout
函数,所以我的对象停留在y<0
上一段时间,那时我想我的增量只触发一次,但它继续触发...更多我使用setTimeout
函数延迟我的函数增加操作的次数得到触发......所以无论我的对象在y<0
停留多长时间,我的增量只触发一次的解决方案是什么
Player.prototype.checkInWater = function () {
if (this.y < 0) {
++scoreGot
setTimeout(function(){
player.x = 202;
nplayer.y = 405;
}, 300);
}
};
Player = function(){
....
this.checkedInWater = false;
}
Player.prototype.checkInWater = function () {
if (this.y < 0 && !this.checkedInWater) {
++scoreGot;
t = this;
t.checkedInWater = true;
setTimeout(function(){
player.x = 202;
nplayer.y = 405;
t.checkedInWater = false;
}, 300);
}
};