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

2014/1/9

Help With game

DoctorrBoss DoctorrBoss

2014/1/9

#
ASAP!! I have a problem in my code. Everything works fine in my game, which is about shooting aliens, and if they reach the land, the game ends. When they reach the land, the game ends, but when I shoot a ship, it says 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.getY(Actor.java:170) at AlienShip.checkIfOnLand(AlienShip.java:85) at AlienShip.onTheLand(AlienShip.java:97) at AlienShip.act(AlienShip.java:21) 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) Heres the code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class aAienShip here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AlienShip extends Enemy
{
    public int lives = 0;

    /**
     * Act - do whatever the aAienShip wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveDownwards();
        explodeIfPotatoHits();
        onTheLand();

    } 

    /**
     * This will make the AlienShip move downwards slowly.
     */
    
    
    public void moveDownwards()

    {
        int x = getX();  
        int y = getY();  
        int ny = getY()+1;  
        setLocation(x,ny) ;  
    }

    /**
     * This checks if there is a potato on the AlienShip.
     * If so, it will remove itself from the world, and create a new ship.
     */
    
    
    public void explodeIfPotatoHits()

    { 

        if(canSee(Potato.class))
        {
            spawnNewShip();
            getWorld().removeObject(this);
        }
    }

    /**
     * This will spawn a new ship if the spawnNewShip method is said.
     */
    
    
    public void spawnNewShip()
    {
        {
            AlienShip newAlienShip;
            newAlienShip = new AlienShip();

            World world;
            world = getWorld();

            int worldWidth = 800;
            int worldHeight = 400;

            int x = Greenfoot.getRandomNumber(worldWidth);
            int y = Greenfoot.getRandomNumber(worldHeight);

            world.addObject(newAlienShip, x, y-200);
        }
    }
    
    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean checkIfOnLand()
    {
        if(getY() >=280)
            return true;
        else
            return false;
    } 
    
     /**
     * If the bollean atWorldEdge replies true, it will remove the potato. 
     */
    
    public void onTheLand()
    {
        if(checkIfOnLand())  
        {  
            gameOver();
            return;
        }  
    }
    
    public void gameOver()  
    {  
        Greenfoot.stop();  
    } 
}
HELP!!!
mjrb4 mjrb4

2014/1/9

#
Your explodeIfPotatoHits(); method might remove the actor from the world, but that won't halt the execution of the act method - it will then call onTheLand(), which calls checkIfOnLand(), which calls getY() - a method that makes no sense to call if the actor isn't in the world (because if it isn't, what's it's Y co-ordinate? What should that method return?) As such if you move explodeIfPotatoHits() to be the last method call in your act method, you should be fine.
DoctorrBoss DoctorrBoss

2014/1/9

#
Thanks, it is working now. I'm a bit of a novice so I don't notice small things :/ Thanks again mjrb4 ;)
You need to login to post a reply.