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

2016/3/16

Ech help me

Toly Toly

2016/3/16

#
My program seems to work just fine and even lets me run it. But after the ball disappears, a terminal window pops up, telling me that it can't access code that it shouldn't even be trying to access. Here is what it says "java.lang.IllegalArgumentException: java.io.FileNotFoundException: Could not find file: 200.png at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:281) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:108) at Points.<init>(Points.java:16) at Board.act(Board.java:71) at greenfoot.core.Simulation.actWorld(Simulation.java:600) at greenfoot.core.Simulation.runOneLoop(Simulation.java:535) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Caused by: java.io.FileNotFoundException: Could not find file: 200.png at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:544) at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:278) ... 7 more 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:897) at GoldBall.crush(GoldBall.java:72) at GoldBall.act(GoldBall.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)" If you need the code let me know.
Toly Toly

2016/3/16

#
Here is the code for the GoldBall and the world
import greenfoot.*; 
import java.util.List;

public class Board extends World
{
    private int board;
    private int tries;
    private int rolls;
    private int score;
    private Text boardLabel;
    private Text tryLabel;
    private Text rollsLabel;
    private Text scoreLabel;
    private int ballsMoving;
    private boolean failed;
    private boolean cleared;
    private boolean displayPoints = false;
    /**
     * Definition of the board setups.
     *   One row defines one board setup.
     *   The first number is the number of rolls for this board.
     *   The 2nd and 3rd are the coordinates for the gold ball.
     *   Following that: every pair places a steel ball, unless:
     *     if the next number is 0, then following is a horizontal bar;
     *     if the next number is 1, then following is a vertical bar
     */
    private static final int[][] setups = {  // board = 640x640
            { 1, 320, 480, 320, 200 },
            { 2, 320, 320, 180, 320, 460, 320 },
            { 2, 320, 480, 200, 200, 440, 200 },
            { 3, 320, 320, 290, 480, 200, 190, 470, 230 },
            { 1, 120, 520, 420, 120, 520, 180 },
            { 2, 320, 480, 320, 180 , 0, 225, 315},        
            { 1, 140, 400, 470, 400 , 1, 320, 433, 0, 320, 100},                          // indirect over 1 bar
            { 1, 320, 480, 320, 90, 320, 200, 320, 310 },
            { 4, 320, 320, 200, 320, 440, 320, 260, 220, 380, 220, 260, 420, 380, 420 },  // six star
            { 1, 300, 520, 90, 320, 300, 100, 0, 390, 320},
            { 2, 360, 500, 90, 340, 325, 100, 570, 250, 0, 360, 330},
            { 2, 320, 320, 450, 95, 560, 140, 190, 545, 80, 500 },
            { 4, 320, 480, 120, 120, 120, 220, 220, 120, 220, 220, 320, 120, 320, 220,    // 10 in 2-rows
                420, 120, 420, 220, 520, 120, 520, 220 },
            { 1, 284, 546, 387, 293 , 1, 75, 320, 0, 320, 75, 0, 420, 380 },              // indirect over 2 bars
            { 3, 536, 130, 146, 508 , 0, 410, 230, 0, 230, 410 },            
            { 4, 320, 135, 110, 200, 530, 200, 240, 470, 400, 470, 0, 320, 340 }, 
        };

    public Board()
    {    
        // Create a new world with 20x20 cells with a cell size of 10x10 pixels.
        super (840, 640, 1);
        Greenfoot.setSpeed (52);
        setPaintOrder (ScoreBoard.class, Points.class, Ball.class, Arrow.class);

        board = 1;
        tries = 3;
        score = 0;

        createCounters ();
        setUp (board);
    prepare();
}

    /**
     * Do the acting for the board: check whether w succeeded or failed.
     */
    public void act() 
    {
        if (cleared) {
            Ball goldBall = getGoldBall();
            if (goldBall != null) {
                addObject (new Points ("200"), goldBall.getX()+70, goldBall.getY()-30);
                addScore (300 + rolls*300); // 100 for clearing the board 
                // 200 for gold ball still on it,
                // plus 300 per unused roll
            }
            else {
                addScore (100 + rolls*300); // 100 for clearing the board
                // plus 300 per unused roll
            }
            cleared = false;
            displayPoints = true;
        }
        else if (displayPoints) {
            if ( pointsImageGone() ) {
                displayPoints = false;
                nextBoard();
            }
        }
        else if (failed) {
            lostBoard ();
        }
    }

    /**
     * Record that a ball has started or stopped moving.
     */
    public void ballMoving(boolean moves)
    {
        if (moves) {
            ballsMoving++;
        }
        else {
            ballsMoving--;
            if (ballsMoving == 0) {           // all movement stopped
                if ( isBoardClear() ) {
                    cleared = true;
                }
                else if (rolls == 0) {
                    failed = true;
                }
                else if (! haveGoldBall()) {
                    failed = true;
                }
            }
        }
    }

    /**
     * Record that a roll has been completed.
     */
    public void countRoll()
    {
        rolls--;
        rollsLabel.setText("Rolls left: " + rolls);
    }

    /**
     * A steel ball dropped off the board.
     */
    public void steelBallDropped()
    {
        addScore(10);
    }

    /**
     * The gold ball dropped off the board.
     */
    public void goldBallDropped()
    {
        // nothing to do - we wait until all movement has stopped before doing anything
        //failed = true;
    }

    /**
     * Check whether the board has been cleared.
     */
    private boolean isBoardClear()
    {
        int balls = getObjects (Ball.class).size();
        int gold = getObjects (GoldBall.class).size();
        return balls - gold == 0;
    }

    /**
     * Check whether we still have the gold ball on the board.
     */
    private boolean haveGoldBall()
    {
        return getGoldBall() != null;
    }

    /**
     * Check whether the board has been cleared.
     */
    private boolean pointsImageGone()
    {
        return getObjects (Points.class).size() == 0;
    }

    private Ball getGoldBall()
    {
        for (Object obj : getObjects(GoldBall.class)) return (Ball)obj;
        return null;
    }

    
    /**
     * Record the fact that an attempt at a board has been unsuccessful.
     * Start over if there are tries left, else game over.
     */
    public void lostBoard() 
    {
        tries--;
        tryLabel.setText ("Tries left: " + tries);
        if (tries == 0) {
            gameOver("Game Over");
        }
        else {
            Greenfoot.delay(100);
            setUp (board);
        }
    }

    /**
     * Game's up.
     */
    public void gameOver(String message) 
    {
        addObject (new ScoreBoard(message, score), 320, getHeight()/2);
        Greenfoot.stop();
    }

    /**
     * Record a score.
     */
    public void addScore(int points)
    {
        score = score + points;
        scoreLabel.setText ("Score: " + score + "      ");
    }

    /**
     * Check whether a given point is off the board.
     */
    public boolean isOffBoard (int x, int y) 
    {
        return (x < 20 || x > 620 || y < 20 || y > 620);
    }

    /**
     * Show the next board (if there is one). Otherwise game's over with a win.
     */
    private void nextBoard()
    {
        Greenfoot.delay(60);
        board++;
        if (board % 3 == 0) {
            tries++;
            tryLabel.setText("Tries left: " + tries);
        }
        if (board <= setups.length) {
            boardLabel.setText("BOARD " + board);
            setUp (board);
        }
        else {
            gameOver ("You win!");
        }
    }

    private void setUp(int boardNumber)
    {
        removeObjects (getObjects (Ball.class));

        int[] current = setups[boardNumber-1];
        int i = 0;
        rolls = current[i++];

        addObject ( new GoldBall(), current[i++], current[i++]);
        rollsLabel.setText ("Rolls left: " + rolls);
        failed = false;
        cleared = false;
        ballsMoving = 0;
    }

    private void createCounters()
    {
        boardLabel = new Text("BOARD " + board);
        addObject (boardLabel, 670, 120);
        tryLabel = new Text("Tries left: " + tries);
        addObject (tryLabel, 670, 150);
        rollsLabel = new Text("Rolls left: " + rolls);
        addObject (rollsLabel, 670, 200);
        scoreLabel = new Text("Score: " + score + "      ");
        addObject (scoreLabel, 670, 230);
    }

/**
 * Prepare the world for the start of the program.
 * That is: create the initial objects and add them to the world.
 */
private void prepare()
{
    Bomb bomb = new Bomb();
    addObject(bomb,127,173);
    Fly fly = new Fly();
    addObject(fly,565,193);
}
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class GoldBall extends Ball
{
    private Arrow arrow;

    public void act() 
    {
        super.act();
        if (! isMoving()) {
            checkDrag();
        }
        crush();
    }

    /**
     * Check whether we have a mouse drag gesture. Then we need to show the force arrow.
     */
    private void checkDrag()
    {
        if(Greenfoot.mouseDragged(this)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            int dx = mouse.getX()-getX();
            int dy = mouse.getY()-getY();
            //             int dx = Math.min (mouse.getX()-getX(), 100);
            //             int dy = Math.min (mouse.getY()-getY(), 100);
            //             dx = Math.max (dx, -100);
            //             dy = Math.max (dy, -100);
            if (arrow == null) {   // just starting to drag now
                arrow = new Arrow(dx, dy);
                getWorld().addObject( arrow, getX(), getY() );
            }
            else {
                arrow.setImage(dx, dy);
            }
        }
        if(Greenfoot.mouseDragEnded(this) && arrow != null) {
            getWorld().removeObject(arrow);
            getBoard().countRoll();
            arrow = null;
            MouseInfo mouse = Greenfoot.getMouseInfo();
            Vector force = new Vector(getExactX() - mouse.getX(), getExactY() - mouse.getY());
            force.scale(0.1);
            addForce (force);
            setMoving(true);
        }
    }

    /**
     * This ball has dropped. Do whatever is necessary.
     */
    public void hasDropped()
    {
        getBoard().goldBallDropped();
    }

    public void crush()
    {
        Actor fly;
        fly = getOneObjectAtOffset(0, 0, Fly.class);
        if (fly != null)
        {
            World world;
            world = getWorld();
            world.removeObject(fly);
        }
    }
}
Toly Toly

2016/3/16

#
Sorry, the first part of my last comment is the code for the world.
danpost danpost

2016/3/16

#
Apparently, the filename is not exactly correct (remember, java is case-sensitive). To investigate the 'IllegalStateException' error, you will need to post the act method of the Ball class.
Toly Toly

2016/3/16

#
Could you explain a bit more please? I understand the java is case-sensitive, I'm just not entirely sure about what you mean when you say "post the act method."
danpost danpost

2016/3/16

#
Toly wrote...
I'm just not entirely sure about what you mean when you say "post the act method."
I mean for you to show its code here in the discussion thread.
Toly Toly

2016/3/17

#
Oh, sorry. Here it is import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; public class Ball extends SmoothMover { private static final double DIAMETER = 54.0; private static final int RADIUS = 27; private static final int BAR_SHADOW = 10; private boolean moving; private boolean out; private boolean haveHitLastAct; // true if last act step was a hit on a bar private GreenfootImage image; /** * Create a ball. */ public Ball() { image = getImage(); } /** * Do the acting. That is: either move the ball or fall down if we're off the board. */ public void act() { if (out) { move(); drop(); } else if (moving) { move(); checkCollision(); accelerate(0.99); // accelerate with factor < 1 - that is: decelerate if (getSpeed() < 0.1) { setMoving(false); } checkBoardBoundary(); } } /** * Check whether we are colliding with another ball. */ private void checkCollision() { // first, get all balls whose image intersects (we don't need to check the others) List<Ball> balls = (List<Ball>) getIntersectingObjects(Ball.class); for (Ball ball : balls) { // of those, see which really intersect properly with the ball itself (the image is // bigger than the ball, so that we can draw the shadow, etc). if ( haveHit(ball) ) { doCollision(ball); } } // Now, check for wooden bars List<Bar> bars = (List<Bar>) getIntersectingObjects(Bar.class); for (Bar bar : bars) { // of those, see which really intersect properly with the bar itself (the image is // bigger than the bar, because it includes the shadow). if ( haveHit(bar) ) { doCollision(bar); } } } /** * We have hit another ball. Perform the collision now (that is: compute the new vectors of * movement for us and the other ball). */ private void doCollision(Ball ball) { double dx = this.getExactX() - ball.getExactX(); double dy = this.getExactY() - ball.getExactY(); int direction = (int) Math.toDegrees(Math.atan2(dy, dx)); double angle = direction - getMovement().getDirection(); // if the not 90 < angle < 270 then we're hit from behind and don't want to move the other ball if (Math.abs(angle) < 90 || Math.abs(angle) > 270) { return; } double length = Math.cos(Math.toRadians(angle)) * getMovement().getLength(); ball.addForce( new Vector (direction, length) ); ball.setMoving(true); this.addForce (new Vector (direction + 180, length) ); //System.out.println("dir (after): " + direction + " " + this); } /** * We have hit another ball. Perform the collision now (that is: compute the new vectors of * movement for us and the other ball). */ private void doCollision(Bar bar) { int dx = Math.abs (this.getX() - bar.getX()) - bar.getImage().getWidth()/2 + BAR_SHADOW - RADIUS; int dy = Math.abs (this.getY() - bar.getY()) - bar.getImage().getHeight()/2 + BAR_SHADOW - RADIUS; boolean hitSide = dx > dy; boolean hitTopBottom = ! hitSide; if (Math.abs(dx-dy) <= 2) { // If they are similar, we have hit the corner hitSide = hitTopBottom = true; } if (hitSide) { // have hit from side getMovement().revertHorizontal(); } if (hitTopBottom) { // have hit from top or bottom getMovement().revertVertical(); } accelerate (0.9); // lose some speed when bouncing off } /** * Check whether we have hit the given ball. We have hit it if its distance from us * (measured at the centre points) is less then our diameter. */ private boolean haveHit(Ball ball) { int dx = Math.abs (this.getX() - ball.getX()); int dy = Math.abs (this.getY() - ball.getY()); double distance = Math.sqrt(dx*dx+dy*dy); return distance < DIAMETER; } /** * Check whether we have hit a ball. We have hit one, if the distance from us (measured at the * centre points) is less then our diameter. */ private boolean haveHit(Bar bar) { if (haveHitLastAct) { // make sure we don't register a hit twice in a row haveHitLastAct = false; return false; } else { int dx = Math.abs (this.getX() - bar.getX()) - bar.getImage().getWidth()/2 + BAR_SHADOW - RADIUS; int dy = Math.abs (this.getY() - bar.getY()) - bar.getImage().getHeight()/2 + BAR_SHADOW - RADIUS; haveHitLastAct = (dx < 0) && (dy < 0); return haveHitLastAct; } } /** * Check whether we've gone over the edge of the board. */ private void checkBoardBoundary() { Board board = (Board) getWorld(); if ( board.isOffBoard(getX(), getY()) ) { out = true; } } /** * Do the drop motion (which is shown when we're off board). This is done by simply scaling down our * picture. If it gets small enough, we disappear from the world. */ private void drop() { if(getImage().getWidth() < 10) { hasDropped(); Board board = getBoard(); board.removeObject(this); board.ballMoving(false); } else { GreenfootImage img = new GreenfootImage(image); img.scale ( getImage().getWidth()-6, getImage().getHeight()-6 ); setImage (img); } } /** * This ball has dropped. Do whatever is necessary. */ public void hasDropped() { Board board = getBoard(); board.steelBallDropped(); int x = Math.max (getX(), 50); int y = Math.max (getY(), 40); y = Math.min (y, board.getHeight()-40); board.addObject (new Points("10"), x, y); } /** * Check whether we're moving. */ public boolean isMoving() { return moving; } /** * Set this ball to moving or not moving state. */ public void setMoving(boolean move) { if (moving != move) { getBoard().ballMoving(move); } moving = move; } /** * Return the board we're on. */ public Board getBoard() { return (Board)getWorld(); } }
danpost danpost

2016/3/17

#
Okay. When the ball drops, it is eventually removed from the world. This is done while 'super.act()' is executed from line 9 of the GoldBall class After line 9, it appears that the condition of the 'if' at line 10, will be 'false' once the ball drops, so no lines requiring the actor be in the world are executed thru line 12. Line 13, however, call a method that needs the actor be in the world to execute properly. Therefore, you need to ensure the actor is in the world before calling the 'crush' method. You can change line 13 to this:
if (getWorld() != null) crush();
Toly Toly

2016/3/18

#
Thanks!
You need to login to post a reply.