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

2014/11/19

losing the game on contact

Favna Favna

2014/11/19

#
Hi guys, First of all I'll mention that I'm still fairly new to GreenFoot. I've progressed pretty far in creating a maze game, but I stumbled upon a pretty odd and especially annoying error. In the maze there are a few bolts flying around as well as a wallmaster walking around. Whenever touching either I remove all objects from the world and show a "you lost the game" screen. Now, for the wallmaster this all works perfectly, but whenever I touch a bolt I get the following error:
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:681) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:885) at Player.canSee(Player.java:109) at Player.deathByWallmaster(Player.java:144) at Player.act(Player.java:42) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
My code is as follows (I shrunk parts down to what would matter) For the Bolt class:
public class Bolt extends Actor
{
    private int movespeed = 1;
    /**
     * Act - do whatever the Bolt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
    }    

    public void move()
    {
        //moves bolt from bottom-right to top-right
        if(getX() >= 75)
        {
            setLocation(getX() + movespeed, getY());
        }

        //moves wallmaster from top-right to top-left
        //         Actor Bolt;
        if(getX() == 165)
        {
            World world = getWorld();
            world.removeObjects(getWorld().getObjects(Bolt.class));
            world.addObject(new Bolt(), 75, 375);
            world.addObject(new Bolt(), 75, 405);
            world.addObject(new Bolt(), 75, 435);
        }
    }
For my Player class:
public class Player extends Actor
{
    public void act() 
    {
        //Calling all methods
        deathByBolts();
    }

    public boolean canSee(Class clss) {  
        return getOneObjectAtOffset(0, 0, clss) != null;  
    }  

    public void itemPick(Class cls) {  
        Actor actor = getOneObjectAtOffset(0, 0, cls);  
        if(actor != null) {
            getWorld().removeObject(actor);  
        }
    }

    public void deathByBolts()
    {
        if(canSee(Bolt.class))
        {
            World world = getWorld();
            world.removeObjects(getWorld().getObjects(null));
            world.addObject(new Lose(), world.getWidth()/2, world.getHeight()/2);
        }
    }
danpost danpost

2014/11/19

#
These lines of the error message:
at Player.canSee(Player.java:109)
at Player.deathByWallmaster(Player.java:144)
at Player.act(Player.java:42)
tells me that there is more to your Player act method than you are showing, which is important. I have a feeling you are calling 'deathByWallmaster' AFTER 'deathByBolts' in the act method; so that when your player 'dies by bolts' you still check (afterwards) for 'death by wallmaster'. However, when you die by bolts, the player is removed from the world and therefore you cannot check for death by wallmaster because there is no world to check in. Try this:
// instead of
deathByWallmaster();
// use this
if (getWorld() != null) deathByWallmaster();
// or insert the following after 'deathByBolts'
if (getWorld() == null) return;
either way, you are preventing 'deathByWallmaster' from being executed if the player is no longer in the world.
Favna Favna

2014/11/19

#
Hm yes actually those 2 lines of deathby were in fact under each other. I have just tested your solution (the first one) and it works. Thanks :)!
You need to login to post a reply.