gpt4 book ai didi

javascript - 检查碰撞功能是否匹配同一位置的 2 个物体

转载 作者:行者123 更新时间:2023-12-03 01:46:39 25 4
gpt4 key购买 nike

完整代码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();
}
}
};

最佳答案

为什么不使用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();
}
}
};

关于javascript - 检查碰撞功能是否匹配同一位置的 2 个物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50603465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com