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

2014/11/5

Enemy health wont work.

Srax Srax

2014/11/5

#
I am making a game, where a space ship shoots an enemy. If you follow my code, the enemy should have 3 health. I can compile the world, but once i shoot the enemy i get this error code: 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:681) at greenfoot.Actor.getX(Actor.java:157) at Mover.atWorldEdge(Mover.java:70) at Bullet.act(Bullet.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Heres the code for the Bullet:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Mover
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(10.0);
       hitEnemy();
       if (this.atWorldEdge())
       {  
           getWorld().removeObject(this);  
       }
    }
    public void hitEnemy() {
        Enemy enemy = (Enemy) getOneObjectAtOffset(0, 0, Enemy.class);
        if (enemy != null) {
            enemy.setHealth(-1);
            
            getWorld().removeObject(this);
        }
    }
}
And here's the code for the enemy
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }
    
    private int health = 5;
    public void setHealth(int points) {
        health += points;
    }
    public int getHealth() {
        return health;
    }
}
Code for spaceship:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Spaceship extends Actor
{
    /**
     * Act - do whatever the Crab wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private float fireTimer;
    public void act()
    {
        moveAndTurn();
        eat();
        if ("space".equals(Greenfoot.getKey()))
        {
            {
                fire1();
                fire2();
            }
    }
    }
    public void moveAndTurn()
    {
        move (0);
        
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            move(-5);
        }
    }
    
    public void eat()
    {
        
    }
    
    private void fire1()
    {
        {
            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, getX(), getY()+52);
        }
    }
    private void fire2()
    {
        {
            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, getX(), getY()-52);
        }
    }
}
danpost danpost

2014/11/5

#
In your Bullet class act method you can the method 'hitEnemy' (line 18) which could remove the bullet from the world; then after that you call 'atWorldEdge' (in line 19) which tries to use the 'getX' and 'getY' methods. If the bullet is removed from within the 'hitEnemy' method, the there is no x or y that 'atWorldEdge' could use because the bullet is not in a world to get an x and y value from.
Srax Srax

2014/11/5

#
danpost wrote...
In your Bullet class act method you can the method 'hitEnemy' (line 18) which could remove the bullet from the world; then after that you call 'atWorldEdge' (in line 19) which tries to use the 'getX' and 'getY' methods. If the bullet is removed from within the 'hitEnemy' method, the there is no x or y that 'atWorldEdge' could use because the bullet is not in a world to get an x and y value from.
Okay, i'm pretty new to Greenfoot (And coding in general) so i don't really know how to fix that :(
davmac davmac

2014/11/5

#
i don't really know how to fix that :(
The problem is that you can't call the atWorldEdge() method once the actor has been removed from the world. The way to fix it is to change your code so that it doesn't call the atWorldEdge() method if the actor has been removed from the world. :) There's a full explanation here. Many other people have experienced this problem. I suggest you try a search, in future, to see if others have encountered your problem and whether they found a solution. In this case you can search the Greenfoot site for the error message, "Actor not in world", for instance.
Srax Srax

2014/11/6

#
davmac wrote...
i don't really know how to fix that :(
The problem is that you can't call the atWorldEdge() method once the actor has been removed from the world. The way to fix it is to change your code so that it doesn't call the atWorldEdge() method if the actor has been removed from the world. :) There's a full explanation here. Many other people have experienced this problem. I suggest you try a search, in future, to see if others have encountered your problem and whether they found a solution. In this case you can search the Greenfoot site for the error message, "Actor not in world", for instance.
Okay, thanks :)
You need to login to post a reply.