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

2013/2/23

The game stops

shovaughn shovaughn

2013/2/23

#
Hi, I have a plane, a bullet, a red balloon and a blue balloon. The plane moves and shoots bullets. When a bullet hits a blue balloon both the bullet and the balloon are removed, which is what I want. When a bullet hits a red balloon both the bullet and the balloon are removed but the game freezes, what have I got wrong? Here is the bullet code: import greenfoot.*; public class Bullet extends Actor { public Bullet(int rotation) { setRotation(rotation); } public void act() { move(10); destroyRedBalloon(); destroyBlueBalloon(); } public void destroyRedBalloon() { Actor RedBalloon; RedBalloon = getOneObjectAtOffset(0, 0, RedBalloon.class); if(RedBalloon != null) { getWorld().removeObject(RedBalloon); getWorld().removeObject(this); } } public void destroyBlueBalloon() { Actor BlueBalloon; BlueBalloon = getOneObjectAtOffset(0, 0, BlueBalloon.class); if(BlueBalloon != null) { getWorld().removeObject(BlueBalloon); getWorld().removeObject(this); } } } Thanks
MatheMagician MatheMagician

2013/2/23

#
What is happening is you are calling the destroyBlueBalloon() after you have already removed the bullet from the world because of destroyRedBalloon(). This causes errors. Replace where you call the destroyBlueBalloon() method with:
if(getWorld() != null) destroyBlueBalloon();
shovaughn shovaughn

2013/2/23

#
Thanks :)
You need to login to post a reply.