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

2015/10/19

Shooting Bullet causes error

ekavanag ekavanag

2015/10/19

#
I try to shoot a bullet but it freezes the game and gives me an error, I encountered this problem before where it would give an error after the target had been hit but now it won't even fire. This is my "note" (bullet) code public class Note extends Player { int direction,speed; /** * Act - do whatever the Note wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { speed=-5; setRotation(direction); move(speed); ifAtEdge(); hitTree(); destroy(); } public void ifAtEdge() { if (atWorldEdge() == true) { getWorld().removeObject(this); } } public boolean atWorldEdge() { if (getX() == 0) { return true; } else { return false; } } public void hitTree() { if (isTouching(Shrub.class)) { getWorld().removeObject(this); } } public void destroy() { if(getOneIntersectingObject(EvilSpirit.class) != null) { getWorld().removeObject(getOneIntersectingObject(EvilSpirit.class)); getWorld().removeObject(this); } } } Please Help!
danpost danpost

2015/10/19

#
If you are to call methods like 'getOneIntersectingObject' or 'isTouching' or even 'getX', the actor must be in the world. You call 'ifAtEdge', 'hitTree' and 'destroy' one after another. Each of these use one of those methods and if any but the last one removes the actor (the bullet) from the world, the following one will throw an error. Use the following after each one of those method calls except the last one:
if (getWorld() == null) return;
ekavanag ekavanag

2015/10/20

#
Thank you! It can shoot without an error now but it freezes the game when a bullet reaches the world edge or hits one of the walls, I'm not sure if i need to us the: if (getWorld() == null) return; somewhere else or why it does this.
ekavanag ekavanag

2015/10/20

#
nevermind I sorted it out
You need to login to post a reply.