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

2013/8/16

Error at Breakout Game

ich99 ich99

2013/8/16

#
Hey Guys, I made a Breakout Game, using the Joy of code on Youtube, now after the last Episode I have a problem, all works fine but when the ball is falling down and don't bouncing up again this Error appears: 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:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Ball.checkBlock(Ball.java:72) at Ball.act(Ball.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Now i looked in the Ball.class at Line 72, but i cant find the mistake! Here i the whole code of the Ball.class:
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
    
    
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            makeSmoke();
            checkOut();
            checkBlock();
        }
    }
    
    /**
     * 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;
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
    }
    
    /**
     * Check wheter we have hit a Block, and make the block disappear if we have.
     */
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        
        if(block != null)
        {
            getWorld().removeObject(block);
            deltaY = -deltaY;
 
        }
    }
    
    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;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Put out a puff of smoke (only on every second call).
     */
    private void makeSmoke()
    {
        count--;
        if (count == 0) {
            getWorld().addObject ( new Smoke(), getX(), getY());
            count = 2;
        }
    }
    
    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
Thanks for help.
Kartoffelbrot Kartoffelbrot

2013/8/16

#
The problem is the order of your methods in your act method! In your checkOut() method the Ball removes, if he is at the bottom of the world. If he removes himself, the act-method will be finished nevertheless. And then it calls the checkBlock() method. But your Ball can't check for any intersecting objects because he isn't in the world. Then you get this error. Put the checkOut method as the last method in your act method. Then it should work.
ich99 ich99

2013/8/17

#
Now my code is :
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
    
    
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            makeSmoke();
            checkOut();
            checkBlock();
        }
    }
    
    /**
     * 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;
        }
    }        
    
    /**
     * Check wheter we have hit a Block, and make the block disappear if we have.
     */
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        
        if(block != null)
        {
            getWorld().removeObject(block);
            deltaY = -deltaY;     
            Board myBoard = (Board) getWorld();
            myBoard.score();
        }        
    }
            
    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;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
            
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Put out a puff of smoke (only on every second call).
     */
    private void makeSmoke()
    {
        count--;
        if (count == 0) {
            getWorld().addObject ( new Smoke(), getX(), getY());
            count = 2;
        }
    }
    
    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }    
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
    }
}
but it is the same Problem as before.
Kartoffelbrot Kartoffelbrot

2013/8/17

#
Because it's the same!? Your act method should be this:
public void act()   
    {  
        if (!stuck)   
        {  
            move();  
            makeSmoke();   
            checkBlock();  
           checkOut();  
        }  
    }  
ich99 ich99

2013/8/17

#
Oh Thank you now it works .
You need to login to post a reply.