【发布时间】:2018-05-26 10:40:34
【问题描述】:
在线性格斗游戏中,我使用下面的公式来获得 X 碰撞和 Y 碰撞(当角色跳到另一个战士的顶部时)。 我真的不知道这个公式有什么问题,因为我在游戏中遇到了一些无意义的逻辑错误。(当发生碰撞时,我会向前和向后移动角色,所以有时我会得到无意义的位置。 除了我的公式,如果你还有其他公式,请分享。谢谢
if (this.x < secondPlayer.x + secondPlayer.width + this.border &&
this.x + this.width + this.border > secondPlayer.x && this.y < secondPlayer.y + secondPlayer.height + this.border &&
this.height + this.y + this.border > secondPlayer.y && this.x != secondPlayer.x){
return 'x';
}
else if (this.x == secondPlayer.x && this.y < secondPlayer.y + secondPlayer.height + this.border &&
this.height + this.y + this.border > secondPlayer.y) {
return 'y';
}else{
return 'n';
}
【问题讨论】:
-
是
this.border一个标量值(例如,23.4)还是一个对象? -
请解释一下“边界”代表什么。此外,您需要在条件中添加更多括号!
-
是的,它的值(不是对象)用于字符边框。
-
@NicolasCailloux:我没有语法错误,但我认为可能在 Y 冲突中,一些逻辑问题,因为它必须在字符之上。
-
单元测试可能会突出这个问题?您可以生成满足 if 中每个条件的测试数据,然后将它们混合并匹配一组不同的条件。
标签: javascript collision game-physics