【发布时间】:2019-01-29 21:27:02
【问题描述】:
我有一个 if 语句,它检查我的对象(向导)是否命中了 item。如果他这样做了,巫师的体型就会改变,他应该能够在与障碍物的 1 次碰撞中幸存下来。
现在我被困在“幸存 1 次障碍物碰撞”,因为在我的碰撞方法中我已经设置它,如果 InvulnerabilityActive 为真,那么它不应该检测到碰撞。
所以问题在于,在整个持续时间(9 秒)内,没有检测到与障碍物的碰撞。巫师刚刚飞过。 如何更改它,使其不会检测到与第一个障碍物的碰撞,然后停用该项目?
我曾想过使用Timer.cancel() 方法,但如您所见,我只能使用this 关键字来引用它。在我使用 Timer 之前,我无法调用 Timer.cancel()。
这是与物品的碰撞。
try {
invulnerability = new Rectangle(GameWorld.obstacle1.getX() - GameRenderer.generator2.getValue2(),
GameWorld.obstacle1.getY() + GameRenderer.generator2.getValue1(), 15, 15);
if ((Intersector.overlaps(GameWorld.wizard.getBoundingRectangle(), invulnerability))){
GameRenderer.InvulnerabilityActive = true;
activeItem = true;
case0 = true;
GameWorld.wizard.setWidth(8);
GameWorld.wizard.setHeight(8);
new java.util.Timer().schedule(
new java.util.TimerTask() {
public void run() {
this.cancel();
GameRenderer.InvulnerabilityActive = false;
activeItem = false;
case0 = false;
GameWorld.wizard.setWidth(16);
GameWorld.wizard.setHeight(16);
}
},
9000
);
}
} catch (NullPointerException e){
System.out.println("Caught NullPointerException!");
}
这是与障碍物碰撞的方法:
public boolean collides(Wizard wizard) {
if (GameRenderer.InvulnerabilityActive){
return false;
} else {
return (Intersector.overlaps(wizard.getBoundingRectangle(), barUp)
|| Intersector.overlaps(wizard.getBoundingRectangle(), barDown));
}
}
我知道问题是由于 if 语句,因为它只是检查项目是否处于活动状态,但我不知道如何更改它以使其工作。
【问题讨论】:
标签: java timer libgdx collision-detection