动画未在 Phaser.js 内启动一次
Animations not starting once in Phaser.js
我用的是phaser 2.6.1.
我制作了一个动画。这工作正常,但有时动画没有开始。不知道为什么。
在 create()
函数中,我为 spritesheet 添加了动画:
this.car = game.add.sprite(game.world.centerX,500, 'car');
this.car.animations.add('mover',[0,1,2,3],1000, true);
this.car.fixedToCamera = true;
当重叠时,我启动了动画功能:
game.physics.arcade.overlap(this.car, this.borders, this.canMove, null, this);
这是我的功能:
},canMove: function(car, objet){
if(crash == 0){
crash = 1;
_this.car.animations.play("mover");
console.log("fire")
setTimeout(function(){
_this.car.animations.stop();
_this.car.frame = 0;
crash = 0;
},1000)
}
}
当汽车与边界重叠时,触发此功能。
触发了console.log()
但是动画突然没有开始。我可以重新启动很多时间我的页面,突然动画没有开始,即使 console.log()
被触发。
问题是因为我首先用 :
初始化了我的车
this.car = game.add.sprite(game.world.centerX,500, 'car');
this.car.animations.add('mover',[0,1,2,3],1000, true);
this.car.fixedToCamera = true;
_this = this
在我的函数中,我直接用 _this
调用我的车
我使用 _this
因为在 setTimeout()
中,this
属性 不等于文档
的 this
我不知道为什么,但如果我先用 this
初始化,然后用 _this
调用,这就无法正常工作。另外,为了解决我的问题,我直接从 _this
开始。
_this = this;
_this.car = game.add.sprite(game.world.centerX,500, 'car');
_this.car.animations.add('mover',[0,1,2,3],1000, true);
_this.car.fixedToCamera = true;
我用的是phaser 2.6.1.
我制作了一个动画。这工作正常,但有时动画没有开始。不知道为什么。
在 create()
函数中,我为 spritesheet 添加了动画:
this.car = game.add.sprite(game.world.centerX,500, 'car');
this.car.animations.add('mover',[0,1,2,3],1000, true);
this.car.fixedToCamera = true;
当重叠时,我启动了动画功能:
game.physics.arcade.overlap(this.car, this.borders, this.canMove, null, this);
这是我的功能:
},canMove: function(car, objet){
if(crash == 0){
crash = 1;
_this.car.animations.play("mover");
console.log("fire")
setTimeout(function(){
_this.car.animations.stop();
_this.car.frame = 0;
crash = 0;
},1000)
}
}
当汽车与边界重叠时,触发此功能。
触发了console.log()
但是动画突然没有开始。我可以重新启动很多时间我的页面,突然动画没有开始,即使 console.log()
被触发。
问题是因为我首先用 :
初始化了我的车this.car = game.add.sprite(game.world.centerX,500, 'car');
this.car.animations.add('mover',[0,1,2,3],1000, true);
this.car.fixedToCamera = true;
_this = this
在我的函数中,我直接用 _this
我使用 _this
因为在 setTimeout()
中,this
属性 不等于文档
this
我不知道为什么,但如果我先用 this
初始化,然后用 _this
调用,这就无法正常工作。另外,为了解决我的问题,我直接从 _this
开始。
_this = this;
_this.car = game.add.sprite(game.world.centerX,500, 'car');
_this.car.animations.add('mover',[0,1,2,3],1000, true);
_this.car.fixedToCamera = true;