1 回答
TA贡献1842条经验 获得超12个赞
您需要先检查碰撞。如果检测到碰撞-检查InvulnerabilityActive。如果是true- 将其重置为false并返回 false。在下一次碰撞InvulnerabilityActive将是假的,碰撞将被检测到。
public boolean collides(Wizard wizard) {
if (Intersector.overlaps(wizard.getBoundingRectangle(), barUp)
|| Intersector.overlaps(wizard.getBoundingRectangle(), barDown)) {
if (GameRenderer.InvulnerabilityActive) {
GameRenderer.InvulnerabilityActive=false;
return false;
} else {
return true;
}
} else {
return false;
}
}
可能会出现“向导飞过障碍物并不断检测到碰撞”之类的问题。在这种情况下:将额外的字段添加bool InvulnerabilityActivePending到GameRenderer. GameRenderer.InvulnerabilityActivePending = true;旁边添加GameRenderer.InvulnerabilityActive = true;。并使用此代码:
public boolean collides(Wizard wizard) {
if (Intersector.overlaps(wizard.getBoundingRectangle(), barUp)
|| Intersector.overlaps(wizard.getBoundingRectangle(), barDown)) {
if (GameRenderer.InvulnerabilityActive) {
GameRenderer.InvulnerabilityActivePending=false;
return false;
} else {
return true;
}
} else {
GameRenderer.InvulnerabilityActive=GameRenderer.InvulnerabilityActivePending;
return false;
}
}
我们不是InvulnerabilityActive在碰撞开始时重置,而是在碰撞结束时重置它。
添加回答
举报