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

2015/1/1

PLEASE HELP!!! Shooting an opponent is failing!

jamesyu415 jamesyu415

2015/1/1

#
Hey Guys, there is a problem thats driving me crazy. 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. This error keeps coming up. The game continues to work well if it hits the monster. But as soon as I miss, the game stops and I get this error. I kinda understand what the problem, but why does the game stop as soon as I miss the monster. The bullet doesnt remove at world edge. Please Help me ASAP
public void act() 
    {
        move(15);
        remove();
        lookForMonster();
        
    }    
    
    public void remove()
    {   
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
            return;
        }    
}

 public void lookForMonster()
    {
        if ( canSee (Monster.class))
        {
            eat (Monster.class);
            Greenfoot.playSound ("Fire.wav");
            getWorld().removeObject(this);
            return;  
        }
        
        if (getWorld().getObjects(Monster.class).isEmpty())
        {
            Greenfoot.stop();
        }
        
    }
    
public Bullet()
{
    setRotation(Greenfoot.getRandomNumber(160)-80);
}
}
danpost danpost

2015/1/1

#
I get the impression that you think that the 'return' statements at the end of your 'remove' and 'lookForMonster' methods will stop the 'act' method from continuing its execution. Well, they do not -- they only cause the current method (the 'remove' or 'lookForMonster') to stop executing and control is passed back to the calling method (the 'act'). The 'return' statement at line 14 does not change anything as it is at the end of the method anyway. If you reverse the 'if' blocks in the 'lookForMonster' method, you can then remove the 'return' statement from it also. Now, you have two calling statements in your 'act' that could potentially have the actor removed from the world. If the first one (regardless of order called) removes the actor, the second one will throw an exception if allowed to execute. You can check the condition of the actor being in the world with:
if (getWorld() != null)
This condition can be used to determine if the second method should be called.
jamesyu415 jamesyu415

2015/1/2

#
So, what should I change? Where do I use "if (getWorld) !=null)"?
danpost danpost

2015/1/2

#
From what was discussed above, what do you think should be in the block of this 'if' conditional statement?
jamesyu415 jamesyu415

2015/1/2

#
if (getWorld() !=null) 
        {
            eat (Monster.class);
            Greenfoot.playSound ("Fire.wav");
            getWorld().removeObject(this);
            return;  
        }
Like this?? Its hard for me to understand, not really experienced haha sorry.
danpost danpost

2015/1/2

#
jamesyu415 wrote...
Like this??
Not quite. First, you did not include the check to see if the actor 'canSee' a monster. Then, again, the 'return' statement is not required. I was referring to where you make calls to two different methods that could potentially remove the actor from the world within the act method. The second call will always be the problem call. So, instead of calling it directly, as in line 4 of your original post, qualify the call (place a condition on calling the method) like this:
if (getWorld() != null) lookForMonster();
jamesyu415 jamesyu415

2015/1/2

#
Thanks the problem is solved. But now, the game stops as soon as I hit one monster. How would I fix that?
danpost danpost

2015/1/2

#
Please show your revised bullet code.
You need to login to post a reply.