【发布时间】:2026-02-08 00:00:02
【问题描述】:
我想要做的是,如果玩家从侧面接触敌人,那么玩家要么失去生命,要么导致游戏结束。 但是如果玩家跳到敌人的头顶,那么它会导致敌人消失。
当玩家在地面上接触敌人时 (玩家y轴等于敌人y轴)...
player.y == enemy.y
和
当玩家跳到敌人头顶时。 (玩家y轴大于敌人y轴)...
player.y > enemy.y
到目前为止,这是我的代码...
Level.prototype.playerTouched = function(type, actor) {
if (type == "enemy" && this.status == null && player.y == enemy.y) {
this.status = "lost";
this.finishDelay = 1;
} else if (type == "enemy" && player.y > enemy.y) {
this.actors = this.actors.filter(function(other) {
return other != actor;
});
}
};
【问题讨论】:
-
有点不清楚你在问什么。这段代码哪些有效,哪些无效?
-
那么问题出在哪里?你在问什么?
-
我相信这是用 javascript player.y 编写此代码的正确方法 敌人.y 玩家 y 大于比敌人 y。我遇到的问题是当我将它们添加到我的代码中时它不起作用。但是,没有它们,代码就可以正常工作。所以我相信我遇到的问题是我没有以正确的方式添加它们。
标签: javascript css html game-physics