[英]Java interrupting/stopping Timer
I have an if statement, that checks if my object (wizard) hit an item
. 我有一个if语句,该语句检查我的对象(向导)是否击中了
item
。 If he did, the size of the wizard will change and he should be able to survive 1 collision with an obstacle. 如果他这样做了,向导的大小将发生变化,他应该能够承受1次障碍物的碰撞。
Right now I'm stuck at "surviving 1 obstacle collision", because in my collision method I have set it up so that if InvulnerabilityActive
is true, then it shouldn't detect collision. 现在,我陷入“幸存1个障碍物碰撞”的困境,因为在我的碰撞方法中,我已经对其进行了设置,因此,如果
InvulnerabilityActive
为true,则它不应检测到碰撞。
So the problem is that, during the entire duration (9 seconds), no collision with an obstacle is being detected. 因此,问题在于,在整个持续时间(9秒)内,没有检测到与障碍物的碰撞。 The wizard just flies through.
向导飞过。 How can I change it, so that it doesn't detect collision with the first obstacle and then deactivates the item?
如何更改它,以使其不会检测到与第一个障碍物的碰撞,然后将其停用?
I thought of using the Timer.cancel()
method, but as you can see I can only refer to it with the this
keyword. 我考虑过使用
Timer.cancel()
方法,但是如您所见,我只能使用this
关键字来引用它。 I can't call Timer.cancel()
before I use the Timer
itself. 在使用
Timer
本身之前,无法调用Timer.cancel()
。
Here is the collision with the item. 这是与物品的碰撞。
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!");
}
Here's the collision with an obstacle method: 这是使用障碍物方法的碰撞:
public boolean collides(Wizard wizard) {
if (GameRenderer.InvulnerabilityActive){
return false;
} else {
return (Intersector.overlaps(wizard.getBoundingRectangle(), barUp)
|| Intersector.overlaps(wizard.getBoundingRectangle(), barDown));
}
}
I know that the problem is due to the if statement, because it just checks whether the item is active or not, but I don't know how to change it to make it work. 我知道问题是由于if语句引起的,因为它只是检查项目是否处于活动状态,但是我不知道如何对其进行更改以使其起作用。
You need to check collisions first. 您需要先检查冲突。 If collision detected - check
InvulnerabilityActive
. 如果检测到冲突,请检查
InvulnerabilityActive
。 If it is true
- reset it to false
and return false. 如果为
true
,请将其重置为false
然后返回false。 On next collision InvulnerabilityActive
will be false and collision will be detected. 在下一次碰撞时,
InvulnerabilityActive
将为false,并且将检测到碰撞。
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;
}
}
There may be problem like "wizard flies through obstacle and collisions are constantly detected". 可能会出现诸如“向导越过障碍物并不断检测到碰撞”之类的问题。 In that case: add extra field
bool InvulnerabilityActivePending
to GameRenderer
. 在这种情况下:向
GameRenderer
添加额外的字段bool InvulnerabilityActivePending
。 Add GameRenderer.InvulnerabilityActivePending = true;
添加
GameRenderer.InvulnerabilityActivePending = true;
next to GameRenderer.InvulnerabilityActive = true;
在
GameRenderer.InvulnerabilityActive = true;
旁边GameRenderer.InvulnerabilityActive = true;
. 。 And use this code:
并使用以下代码:
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;
}
}
Instead of resetting InvulnerabilityActive
on collision start, we reset it when collision ends. 我们不会在碰撞开始时重置
InvulnerabilityActive
,而是在碰撞结束时将其重置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.