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

2017/3/2

Health Bar problems

Ruv25808 Ruv25808

2017/3/2

#
So i don't see anything wrong with this code for my health bar but when i run the scenario and i shoot the boss, it gives me an error saying that an actor isn't in the world when I'm trying to call on it. This is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;  // Need to for fill color of health bar

/**
 * Write a description of class HealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss2HealthBar extends HealthBars
{
    private int health = 100;  // Say, they start with 100 health

    public Boss2HealthBar()
    {

    }

    protected void addedToWorld(World w)
    {
        updateHealthBar(0);        
    }

    public int updateHealthBar(int healthChange)
    {
        health = health - healthChange;  // Note: you can add health by passing in a negative change
        if(health <= 0 )
        {
            return 0;
        }
        else if(health > 100 )
        {
            health = 100;  // can't have more than the max health
        }

        // Redraw health bar to match current health
        GreenfootImage hb = new GreenfootImage(health, 20);
        hb.setColor(Color.GREEN);
        hb.fill();
        setImage(hb);
        setLocation(getImage().getWidth()/2 +250, getY());

        return health;
    }

    public int health()
    {
        return health;
    }
}
danpost danpost

2017/3/2

#
Please show the actual error output trace. Also, destroy your secondary discussion thread
Ruv25808 Ruv25808

2017/3/2

#
This is the error: 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:711) at greenfoot.Actor.getY(Actor.java:177) at Boss2HealthBar.updateHealthBar(Boss2HealthBar.java:40) at Boss2_1.handleCollisions(Boss2_1.java:35) at Boss2_1.act(Boss2_1.java:24)
Ruv25808 Ruv25808

2017/3/2

#
here is the other class if u want to look at it:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Boss2_1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss2_1 extends SecondBosses
{
    int collisionCountdown = 0;
    /**
     * Act - do whatever the Boss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if( collisionCountdown > 0 )
        {
            collisionCountdown--;
        }
        else
        {
            handleCollisions();
        }
    }    

    private void handleCollisions()
    {
        Actor shot = getOneIntersectingObject(Shot.class);
        if(shot != null )
        {
            Boss2World w = (Boss2World) getWorld();
            Boss2HealthBar boss2healthbar = w.getBoss2HealthBar();
            boss2healthbar.updateHealthBar(10);
            collisionCountdown = 20;
            if(boss2healthbar.health() <= 0)
            {
                getWorld().removeObject(this);
            }
        }
    }
}
Ruv25808 Ruv25808

2017/3/2

#
it says the error is coming from the setLocation method and I don't know why.
Super_Hippo Super_Hippo

2017/3/2

#
The problem is (probably) in your Boss2World class.
Ruv25808 Ruv25808

2017/3/2

#
here it is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Boss2World here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss2World extends World
{
    Boss2HealthBar boss2healthbar = new Boss2HealthBar();
    /**
     * Constructor for objects of class BossWorld.
     * 
     */
    public Boss2World()
    {
        super(600, 600, 1);
        prepare();
    }
    
    public Boss2HealthBar getBoss2HealthBar()
    {
        return boss2healthbar;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Boss2_1 boss2_1 = new Boss2_1();
        addObject(boss2_1,300,200);
        Nyancat nyancat = new Nyancat();
        addObject(nyancat, 300, 500);
        FlareTurret flareturret = new FlareTurret();
        addObject(flareturret,340,230);
        FlareTurret flareturret2 = new FlareTurret();
        addObject(flareturret2,260,230);
        Boss2HealthBar boss2healthbar = new Boss2HealthBar();
        addObject(boss2healthbar,300,80);
    }
}
Super_Hippo Super_Hippo

2017/3/2

#
Remove line 41.
Ruv25808 Ruv25808

2017/3/2

#
it worked thanks so much i feel dumb now cuz i fixed that same problem for my other boss fight lol XD.
You need to login to post a reply.