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

2019/3/3

(Frogger) Falling into the water raises an error and almost crashes Greenfoot

444Jam444 444Jam444

2019/3/3

#
For some reason, when this code is run, if the player jumps into the water, an error is raised: " 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:714) at greenfoot.Actor.getOneIntersectingObject(Actor.java:965) at Frog.checkCollision(Frog.java:175) at Frog.checkLocation(Frog.java:159) at Frog.act(Frog.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) " Here is my code: Frog
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Frog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frog extends Player
{
    public int lives = 3;
    public Frog(){
    }
    
    private GreenfootSound runningWater = new GreenfootSound("runningWater.wav");
    private GreenfootSound traffic = new GreenfootSound("traffic.wav");
    private GreenfootSound hop = new GreenfootSound("hop.wav");
    private GreenfootSound carCrash = new GreenfootSound("carCrash.wav");
    public int delayCount = 0;
    public int moveOnLog = 0;
    
    /**
     * Act - do whatever the Frog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (lives > 0){
            checkKeypress(); //raises error if player jumps into water
            checkLocation(); //raises error if player jumps into water
        }
    }
    public void checkKeypress()
    {
        hop.setVolume(50);
        if (delayCount == 0)
        {
            if (Greenfoot.isKeyDown("up"))
            {
                setImage("Frog2.png");
                setLocation(getX(),(getY()-44));
                if (getY() < 45)
                {
                    Greenfoot.stop();
                }
                delayCount = delayCount + 20;
                hop.play();
            }
            if (Greenfoot.isKeyDown("down"))
            {
                setImage("Frog2down.png");
                setLocation(getX(),(getY()+44));
                if (isAtEdge())
                {
                    setLocation((getX()),(getY()-22));
                }
                delayCount = delayCount + 20;
                hop.play();
            }
            if (Greenfoot.isKeyDown("left"))
            {
                setImage("Frog2left.png");
                setLocation((getX()-44),(getY()));
                if (isAtEdge())
                {
                    setLocation((getX()+22),getY());
                }
                delayCount = delayCount + 20;
                hop.play();
            }
            if (Greenfoot.isKeyDown("right"))
            {
                setImage("Frog2right.png");
                setLocation((getX()+44),(getY()));
                if (isAtEdge())
                {
                    setLocation((getX()-22),getY());
                }
                delayCount = delayCount + 20;
                hop.play();
            }
        }
        if (delayCount > 0)
        {
            delayCount = moveDelay(delayCount);
        }
        move(-moveOnLog);
    }
    public static int moveDelay(int delay)
    {
        delay--;
        return delay;
    }
    public void checkLocation()
    {
        //audio that plays based on where the player is in the world.
        if (getY() < 350){
            runningWater.playLoop();
            if (getY() < 236){
                runningWater.setVolume(25);
            }
            else{
                runningWater.setVolume(5);
            }
        }
        else{
            runningWater.stop();
        }
        if (getY() > 199){
            traffic.playLoop();
        }
        else{
            traffic.stop();
        }
        runningWater.setVolume(25);
        traffic.setVolume(25);
        
        if (getY() <= 210){
            if (getY() > 43){ //the river is between these coords
                if (isTouching(Log.class) == false){
                    //player falls off log and dies
                    getWorld().addObject(new FrogSplat(),getX(),getY());
                    Greenfoot.playSound("splash.wav");
                    lives--;
                    respawnPlayer();
                    
                }
                else{
                    //Check which log the frog is on, and move with that log
                    
                    if (getY() == 198){
                        moveOnLog = 2;
                    }
                    if (getY() == 154){
                        moveOnLog = 3;
                    }
                    if (getY() == 110){
                        moveOnLog = 1;
                    }
                    if (getY() == 66){
                        moveOnLog = 4;
                    }
                }
                
            }
            else{
                Greenfoot.playSound("Applause.wav"); //Win
            }
        }
        else{
            moveOnLog = 0;
            //Stops player from being moved left after jumping off a log.
        }
        
        checkCollision();
    }
    public void respawnPlayer(){
        if (lives > 0){
            traffic.stop();
            runningWater.stop(); // when the player dies, position-based audio stops and resets
            setLocation(286,550); // "respawning"
        }
        else{ //Game over!
            getWorld().addObject(new GameOverScreen(),286,286);
            getWorld().removeObject(this);
        }
        
        
    }
    public void checkCollision(){
        Car car = (Car) getOneIntersectingObject(Car.class);
        if (car != null){
            carCrash.setVolume(65);
            carCrash.play();
            getWorld().addObject(new FrogSplat(),getX(),getY()); //skull&crossbones image
            lives = lives - 1;
            respawnPlayer();
            
        }
    }
}
Errors raised in act() method, as pointed out by the single-line comments What can I do to prevent these errors from occurring? I know it's because the 'actor is not in world', but shouldn't "if (lives > 0)" stop those 2 methods from running?
444Jam444 444Jam444

2019/3/3

#
Edit: The error is only raised if the player jumps into the water on their last life, because that's the only time the frog is actually removed from the world
444Jam444 444Jam444

2019/3/3

#
Wait... I'm going to try to teleport the actor to the start position, then, in the act method, remove the actor there instead. Result: Solved! New code:
public void act() 
    {
        if (lives > 0){
            checkKeypress(); //raises error if player jumps into water
            checkLocation(); //raises error if player jumps into water
        }
        else{
            getWorld().removeObject(this);
        }
    }
and as for respawnPlayer():
public void respawnPlayer(){
        if (lives > 0){
            traffic.stop();
            runningWater.stop(); // when the player dies, position-based audio stops and resets
            setLocation(286,550); // "respawning"
        }
        else{ //Game over!
            getWorld().addObject(new GameOverScreen(),286,286);
            setLocation(286,550);
            
        }
444Jam444 444Jam444

2019/3/3

#
Case closed haha.
You need to login to post a reply.