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

2015/5/24

gameError

Jeph Jeph

2015/5/24

#
newRed() work fine but newBlue() does not and the error only happens when i press spacebar and thats in newRed()
 public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    public void newRed()//works fine
    {
        if(canSee(Player2.class)&&Greenfoot.isKeyDown("space"))
        {
            World k = getWorld();
            k.addObject(new RedBlock(),getX(),getY());
            Greenfoot.delay(5);
            k.removeObject(this);

        }

    }

    public void newBlue()//error-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.
    {
        if(canSee(Player1.class)&&Greenfoot.isKeyDown("enter"))
        {
            //getWorld();
            getWorld().addObject(new BlueBlock(),getX(),getY());
            Greenfoot.delay(5);
            getWorld().removeObject(this);

        }
    }
Super_Hippo Super_Hippo

2015/5/24

#
You probably call both methods from the act method. If 'newRed' removes the object from the world, 'canSee' can't work in the 'newBlue' method because it needs the object to be in the world.
Jeph Jeph

2015/5/25

#
thanks i used else if in the act()
You need to login to post a reply.