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

2014/4/18

Actor not in world error

patrick26 patrick26

2014/4/18

#
Hi, No matter how I rearrange the code, I get the same error message. I can't seem to remove the fireball whenever it intersects with other object, and also make it disappear when it reaches the right edge of the world. Is there any way to fix this problem? My code is shown below. public class Fireball extends Actor { private static final int SPEED=7; /** * Act - do whatever the Fireball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(SPEED); Actor goomba = getOneIntersectingObject(Goomba.class); if(goomba!=null) { getWorld().addObject(new Explosion(),getX(),getY()); getWorld().removeObject(this); } Blocks rock = (Blocks)getOneIntersectingObject(Blocks.class); if(rock!=null) { rock.explode(); getWorld().removeObject(rock); getWorld().removeObject(this); } else{ if(getX()>getWorld().getWidth()-3) { getWorld().removeObject(this); } } }
danpost danpost

2014/4/18

#
Add a 'return;' line after each 'getWorld().removeObject(this);' line.
patrick26 patrick26

2014/4/18

#
I also get this error in my Bowser class. It indicates that there is a problem with the 'public Boolean onAnything()' constructor and my 'if(!onAnything()) {fall()};' method. Do you know how to fix this error? Thanks for your help. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Bowser extends Actor { private final double GRAVITY = 1.0; private final int JUMP_STRENGTH = 17; private double vSpeed = 0.0; private int speed = 5; private boolean firing = false; private GreenfootImage image1 = new GreenfootImage("Bowser1.png"); private GreenfootImage image2 = new GreenfootImage("Bowser2.png"); private GreenfootImage image3 = new GreenfootImage("Bowser3.png"); private GreenfootImage image4 = new GreenfootImage("Bowser4.png"); private int k = 0; private int level = 1; public void act() { if(Greenfoot.isKeyDown("left")) { setLocation(getX()-3,getY()); if(getImage()==image3) { setImage(image4); }else { setImage(image3); } } if(Greenfoot.isKeyDown("right")) { setLocation(getX()+3,getY()); if(getImage()==image1) { setImage(image2); }else { setImage(image1); } } if(Greenfoot.isKeyDown("up")) { jump(); } if(Greenfoot.isKeyDown("space")) { if(!firing){ if(getImage()==image1) { getWorld().addObject(new Fireball(), getX(),getY()); Greenfoot.playSound("fireball.wav"); firing = true; } if(getImage()==image2) { getWorld().addObject(new Fireball(), getX(),getY()); Greenfoot.playSound("fireball.wav"); firing = true; } if(getImage()==image3) { getWorld().addObject(new Fireball2 (), getX(),getY()); Greenfoot.playSound("fireball.wav"); firing = true; } if(getImage()==image4) { getWorld().addObject(new Fireball2 (), getX(),getY()); Greenfoot.playSound("fireball.wav"); firing = true; } } } else { firing = false; } Actor goldenkey = getOneIntersectingObject(Goldenkey.class); if(goldenkey!=null) { k = 1; getWorld().removeObject(goldenkey); Greenfoot.playSound("Fairy Dust.wav"); } if(k==1) { Actor greentube = getOneIntersectingObject(Greentube.class); if(greentube!=null) { Greenfoot.playSound("greentube.wav"); Greenfoot.setWorld(new MarioWorld2(this)); level = 2; } } if(level==2) { Actor spikes = getOneIntersectingObject(Spikes.class); if(spikes!=null) { getWorld().addObject(new Poof(),getX(),getY()); getWorld().removeObject(this); } } if(level==1) { Actor goomba = getOneIntersectingObject(Goomba.class); if(goomba!=null) { getWorld().addObject(new Poof(),getX(),getY()); getWorld().removeObject(this); Greenfoot.stop(); } } if(!onAnything()) { fall(); } } public void jump() { if(onAnything()) { vSpeed = -JUMP_STRENGTH; fall(); } } public void fall() { vSpeed = vSpeed + GRAVITY; setLocation( getX(), (int)(getY()+vSpeed) ); if(getY() > getWorld().getHeight()-5) { getWorld().removeObject(this); } } public boolean onAnything() { int dy = getImage().getHeight()/2; Actor ground = getOneObjectAtOffset(0,dy,null); return ground != null; } }
danpost danpost

2014/4/18

#
That is probably the same type problem as before. Using 'Greenfoot.stop()' or 'getWorld().removeObject(this)' does not stop the rest of the method from being executed. With 'getWorld().removeObject(this)', the hidden field that holds the world that 'getWorld()' returns is turned to 'null'. No method that requires the actor be in the world can therefore be executed after it. You must either force an exit from the method by a 'return' statement or check to see if the actor is in a world before executing the following blocks of code:
// either (forcing an exit from the method)
    getWorld().removeObject(this);
    return;
}
if (!onAnything) // etc.

// or (checking if in a world before executing block)
    getWorld().removeObject(this);
}
if (getWorld != null && !onAnything) // etc.
You need to login to post a reply.