Javascript 如何在移相器中设置计数器';更新方法是否正确?
我不熟悉Phaser。代码段如下所示,此处的collisionCnt全局初始化为0:Javascript 如何在移相器中设置计数器';更新方法是否正确?,javascript,html,phaser-framework,Javascript,Html,Phaser Framework,我不熟悉Phaser。代码段如下所示,此处的collisionCnt全局初始化为0: update: function() { this.collisionChecker = this.physics.arcade.collide(this.ground, this.rainGroup, this.over, this.overCheck, this); //checks for collision between rain drops and ground
update: function() {
this.collisionChecker = this.physics.arcade.collide(this.ground, this.rainGroup, this.over, this.overCheck, this); //checks for collision between rain drops and ground
this.physics.arcade.collide(this.rainGroup, this.rainGroup); //checks for collision between rain drops
},
overCheck: function() {
collisionCnt++;
if(collisionCnt == 4) {
console.log(collisionCnt);
return true;
}
else {
console.log(collisionCnt);
return false;
}
},
over: function() {
this.state.start('gameOver');
}
问题是update方法连续监视碰撞实例并连续返回true,导致单个碰撞事件的collisionCnt变为等于4。在比赛结束前,我需要至少4个rainGroup的物体接触地面。欢迎所有帮助并提前感谢:)解决此问题的一个方法是为您的雨滴提供一个属性,该属性将指示雨滴是否碰撞或接触地面。然后在您的检查中查看该属性是否已设置,如果未设置,请递增计数器并设置该属性 我已经说过,但是您的代码中的相关代码更改可能是这样的:
this.collisionChecker = this.physics.arcade.collide(this.ground, this.rainGroup, this.over); //checks for collision between rain drops and ground
over: function(ground, rainDrop) {
if (!rainDrop.hasTouchedGround) {
collisionCnt++;
rainDrop.hasTouchedGround = true;
if (collisionCnt >= 4) {
this.state.start('gameOver');
}
}
}
第二种选择是在雨滴接触地面时消灭雨滴。这会使事情变得更简单,但会将精灵从显示器上移除
over: function(ground, rainDrop) {
collisionCnt++;
rainDrop.kill();
if (collisionCnt >= 4) {
this.state.start('gameOver');
}
}