This site requires JavaScript, please enable it in your browser!
Greenfoot back
hoegie
hoegie wrote ...

2016/12/27

Timer method for hit enemy

1
2
danpost danpost

2017/1/2

#
hoegie wrote...
I have altered it a bit because I want the turtle to change it´ s animation as long as the timer runs. In the animationmethod "animateTurtleHit()" you can see that I work with a Boolean value "turtleHit" The idea is to use it as a switch to animateTurtleHit (timed method) or to animate a normal turtle. When the turtle is hit, the animation lasts 100 counts and the returns to normal animation. I can make switch it on, I can switch it off, but I can not use it within the timer?
Actually, you can use the timer instead of the boolean field. If the timer is not zero, then the turtle was hit:
// instead of 
if (turtleHit)

// use
if (turtleTimer > 0)
In fact you could, instead of calling 'animateTurtleHit' from the act method, call it from the 'checkTurtleHit' method when you decrement the timer. Then you just need to ask about the 'moveRight' value in the method as you already would then know that the turtle was hit.
Is there any way of avoiding this line of code or using something different? I have to explain to my teacher every code use but this is a bit too difficult for me to explain < Code Omitted >
Let me explain in a bit more detail. The Actor class code contains an instance reference field for the world the actor is currently in; namely:
/** Reference to the world that this actor is a part of. */
World world;
When the actor is not in a world, its value is 'null'; otherwise, it references the world the actor is in. The Actor class method 'getWorld' returns the reference value of the field 'world' (a simple 'getter' method). You can check to see if an actor is in a world by checking the value returned by 'getWorld' to 'null'. The 'return' statement causes an immediate end to the method execution -- going back to the code where the method was called from.
hoegie hoegie

2017/1/2

#
a big thank you!
hoegie hoegie

2017/1/2

#
So if I understand correctly, the code "if (getWorld() == null) return;" is used to stop the code from working the moment the turtle has left the world?
danpost danpost

2017/1/2

#
hoegie wrote...
So if I understand correctly, the code "if (getWorld() == null) return;" is used to stop the code from working the moment the turtle has left the world?
Yes. If any command, such as 'getX', getY', 'move', setLocation' or any collision checking methods, are called when the actor is not in the world, you will get a run-time error (IllegalStateException) and the program will stop with a terminal message. If you try to execute ANY world class method on 'getWorld' (for example: getWorld().getObjectsAt(..)) when the actor is not in the world, you will get a run-time error (NullPointerException) and the program will halt with a terminal message. So, to keep things running smoothly after the actor is removed from the world, the execution of these types of codes must be avoided.
You need to login to post a reply.
1
2