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

2014/2/23

java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. HELP

Trxnce Trxnce

2014/2/23

#
Hi guys, i am getting this error when i try to remove my character after the game is finished. the full error i get is: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Actions.isOnPlatform(Actions.java:79) at Mario.act(Mario.java:44) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) the error is coming from my Mario class im sure of it so here is the code in my Mario class
import greenfoot.*; 

public class Mario extends Actions
{
  
    int life = 4;
    boolean fire=false;
    int coins = 0;
    int delay = 0;
    int direction = 0;
    Counter score = null;
    Counter2 clife = null;

 
    
public void act() 
{

 delay ++;
    if (score == null) { score = new Counter ("Score: ");
                         getWorld().addObject(score, 100,20);
                        }
    if (clife == null ) { clife = new Counter2 ("Life: ");
                         getWorld().addObject(clife, 250,20);
                         clife.add(4);
                        }
    if(delay > 100) {fire=true; delay=10;}
    if(life==0){getWorld().removeObject(this);Greenfoot.playSound("Trumpet.mp3"); javax.swing.JOptionPane.showMessageDialog(null, "You died. Game over. "); Greenfoot.stop(); }else {
                   if(canSee(danger.class)) {life = life -1; eat(danger.class); Greenfoot.playSound("Explosion.wav"); clife.subtract(1); }                                                                                                                  
                   if(canSee(life.class)) {life = life +3; eat(life.class); clife.add(3); Greenfoot.playSound("Life.mp3");}
                   if(canSee(coins.class)) { coins = coins+1;eat(coins.class); score.add(5); Greenfoot.playSound("Coin.mp3");}}
if(Greenfoot.isKeyDown("right") & !isOnPlatform(Platform.class,getImage().getWidth()/2,0)) {setImage("2.png");{ direction=0;setLocation(getX()+3,getY());}}
if(Greenfoot.isKeyDown("left") & !isOnPlatform(Platform.class,getImage().getWidth()/2,0)) {setImage("1.png");{ direction=1;setLocation(getX()-3,getY());}} 
//if(Greenfoot.isKeyDown("up"))
if(Greenfoot.isKeyDown("down") & !isOnPlatform(Platform.class,0,getImage().getHeight()/2)) setLocation(getX(),getY()+3);
if(Greenfoot.isKeyDown("space")&fire) {getWorld().addObject(new Bullet(direction), getX(), getY()); Greenfoot.playSound("Bullet.mp3"); fire=false;{ eat(danger.class); Greenfoot.playSound("Explosion.wav"); {getWorld().addObject(new danger(), Greenfoot.getRandomNumber(600), 0);}}


if(getY()<337 & !isOnPlatform(Platform.class,0,getImage().getHeight()/4)) setLocation(getX(),getY()+1);delay(25);}    


 } 
public void delay(int time) {

for (int i=0 ;i<time*1000;i++) {};
}}
Zamoht Zamoht

2014/2/23

#
You should change the code slightly:
if(life==0){getWorld().removeObject(this);Greenfoot.playSound("Trumpet.mp3"); javax.swing.JOptionPane.showMessageDialog(null, "You died. Game over. "); Greenfoot.stop();return; }else {  
                   if(canSee(danger.class)) {life = life -1; eat(danger.class); Greenfoot.playSound("Explosion.wav"); clife.subtract(1); } 
I added a return statement at the end. This way the rest of the act code when get executed and you won't get the error.
Trxnce Trxnce

2014/2/23

#
It worked, cheers mate
You need to login to post a reply.