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

2013/5/12

Actor not in world help

GreenGoo GreenGoo

2013/5/12

#
I am going to upload a game that I have been working on which has an issue I cannot seem to fix. Please can someone go over the code to see my mistake(s)? http://www.greenfoot.org/scenarios/8320
You need to make sure that any line that removes an actor from its world is either followed by a return statement (if in the act method) or is the last line of code gone through act. Otherwise, the actor is trying to do stuff in the world, when it's not in the world. Change this in your square class:
if (inWorld == true && atWorldEdge() == true && inWorldDelay <= 0)
        {
            ((MazeWorld) getWorld()).shapesInWorld--;
            getWorld().removeObject(this);
            inWorld = false;
        } 
if (atWorldEdge() == false && mouse.getX() == this.getX() && mouse.getY() == this.getY() && colourChance == 0 && inWorld == true)
        {
            getWorld().removeObject(this);
            ((MazeWorld) getWorld()).shapesInWorld--;
            inWorld = false;
        }
to
if (inWorld == true && atWorldEdge() == true && inWorldDelay <= 0)
        {
            ((MazeWorld) getWorld()).shapesInWorld--;
            getWorld().removeObject(this);
            inWorld = false;
        } 
else if (atWorldEdge() == false && mouse.getX() == this.getX() && mouse.getY() == this.getY() && colourChance == 0 && inWorld == true)
        {
            getWorld().removeObject(this);
            ((MazeWorld) getWorld()).shapesInWorld--;
            inWorld = false;
        }
with the else statement. Or you can make the first if statement the last if statement.
I've tried the above code with your scenario, and it works, I got no error.
GreenGoo GreenGoo

2013/5/12

#
Thanks!
danpost danpost

2013/5/12

#
There are many things that can be improved upon. Using 'getObjects(Shape.class).size()' would be better than having a field keep track of objects in the world. Having 'inWorld' in the actor class is meaningless as all actors that are acted on ARE in the world. Line 10 above, will still error when the shape is removed by the mouse (switch the order of lines 9 and 10). I am not sure if you want a constant value for 'colourChance' or not; but, if the color of the squares are to change back and forth during execution, remove the field and use 'Greenfoot.getRandomNumber(2)<1' anywhere in the code that 'colourChance' appears.
You need to login to post a reply.