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

2017/5/13

getting error in game

skitlesas skitlesas

2017/5/13

#
I have problem with game... When ball touch ground i get this error: Ball:
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:711)
	at greenfoot.Actor.getOneIntersectingObject(Actor.java:958)
	at Ball.checkBlock(Ball.java:68)
	at Ball.act(Ball.java:34)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
Ball:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    private int count = 2;
    
    private boolean stuck = true;   // stuck to paddle
    private int counter;
    private int counter1;
    
    public Ball()
    {
        counter = 0;
        counter1 = 30;
    }
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            checkOut();
            checkBlock();
            if(counter1 <= 0)
            {
                getWorld().removeObject(this);
                getWorld().addObject(new YouLose(),228, 234);
            }
        }
    }
    
    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }
    
    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }
    
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        if(block != null)
        {
            getWorld().removeObject(block);
            deltaY=-deltaY;
            counter++;
            Board myBoard = (Board) getWorld();
            myBoard.score();
            if(counter >= 18)
            {
                getWorld().addObject(new YouWon(), 228, 234);
                getWorld().removeObject(this);
            }
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
    }
    
    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;
            int offset = getX() - paddle.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX > 7) {
                deltaX = 7;
                counter1 = counter1--;
            }
            if (deltaX < -7) {
                deltaX = -7;
                counter1 = counter1--;
            }
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }
    
    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
danpost danpost

2017/5/13

#
Line 37 removes it from the world. Then, immediately on line 38 you try to get the world it I in; but, it is not in a world anymore.. Switch the two lines.
skitlesas skitlesas

2017/5/13

#
I trying like you said:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    private int count = 2;
    
    private boolean stuck = true;   // stuck to paddle
    private int counter;
    private int counter1;
    
    public Ball()
    {
        counter = 0;
        counter1 = 30;
    }
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            checkOut();
            checkBlock();
            if(counter1 <= 0)
            {
                getWorld().addObject(new YouLose(),228, 234);
                getWorld().removeObject(this);
            }
        }
    }
    
    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }
    
    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }
    
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        if(block != null)
        {
            getWorld().removeObject(block);
            deltaY=-deltaY;
            counter++;
            Board myBoard = (Board) getWorld();
            myBoard.score();
            if(counter >= 18)
            {
                getWorld().addObject(new YouWon(), 228, 234);
                getWorld().removeObject(this);
            }
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
    }
    
    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;
            int offset = getX() - paddle.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX > 7) {
                deltaX = 7;
                counter1 = counter1--;
            }
            if (deltaX < -7) {
                deltaX = -7;
                counter1 = counter1--;
            }
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }
    
    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
but i still getting 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:711)
	at greenfoot.Actor.getOneIntersectingObject(Actor.java:958)
	at Ball.checkBlock(Ball.java:68)
	at Ball.act(Ball.java:34)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/5/13

#
Not sorry, just corrected another one (you would end up getting after correcting this one). With the error you are currently getting, you can fix it by inserting the following at line 34:
if (getWorld() == null) return;
This exits the act method if the 'checkOut' method removes the actor from the world (preventing any further code in the method from executing for this actor, at this time).
Yehuda Yehuda

2017/5/14

#
You can't call the getOneIntersectingObject method if the actor is not in the world (line 68). Calling that method calls a different method in greenfoot.Actor which looks like this:
    /**
     * Throws an exception if the actor is not in a world.
     * 
     * @throws IllegalStateException If not in world.
     */
    private void failIfNotInWorld()
    {
        if(world == null) {
            throw new IllegalStateException(ACTOR_NOT_IN_WORLD);
        }
    }
So since the actor wasn't in the world it threw the exception.
You need to login to post a reply.