完整代码here
我注意到如果在同一个位置有两个错误并且玩家发生碰撞,它会因为它在技术上与两个不同的物体发生碰撞而计算两次死亡,我该如何改变它以检查碰撞1一个时间,还是只算1死?
checkCollision(playerl,playerr) {
//check collision for each bug
for (var i = 0; i < 5; i++) {
var thisEnemy = allEnemies[i];
if (
thisEnemy.leftLimit < player.rightLimit &&
thisEnemy.rightLimit > player.leftLimit &&
thisEnemy.upperLimit > player.lowerLimit &&
thisEnemy.lowerLimit < player.upperLimit) {
console.log("collision");
//console.log(player.lowerLimit, player.upperLimit, thisEnemy.lowerLimit, thisEnemy.upperLimit)
player.loseLife();
}
}
};
答案 0 :(得分:1)
为什么不在死亡一次后使用setTimeout来实现短暂的无敌?
//somewhere in your code
var dying = false;
checkCollision(playerl,playerr) {
//check collision for each bug
for (var i = 0; i < 5; i++) {
var thisEnemy = allEnemies[i];
if (
thisEnemy.leftLimit < player.rightLimit &&
thisEnemy.rightLimit > player.leftLimit &&
thisEnemy.upperLimit > player.lowerLimit &&
thisEnemy.lowerLimit < player.upperLimit &&
dying==false) {
console.log("collision");
dying=true; // dying is true, so we wont end up in this block again
setTimeout(function(){
dying=false; //after 500 milliseconds we set dying to false so our player has the ability to die again!
},500);
player.loseLife();
}
}
};