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

2014/5/22

Healthbar code not working...

1
2
danpost danpost

2014/5/26

#
SAAEngineer wrote...
It extends Actor
Well, then the problem must be in the SpaceShip class; unless you have code in some other class that adds a healthbar to the world at the same location. After compiling, drag the healthbar around to see if another one is behind it. Report back.
SAAEngineer SAAEngineer

2014/5/26

#
danpost wrote...
SAAEngineer wrote...
It extends Actor
Well, then the problem must be in the SpaceShip class; unless you have code in some other class that adds a healthbar to the world at the same location. After compiling, drag the healthbar around to see if another one is behind it. Report back.
yes, there a A LOT of layers behind it.
danpost danpost

2014/5/26

#
SAAEngineer wrote...
yes, there a A LOT of layers behind it.
Then search for where you have any other occurrences of 'new Healthbar' and remove them and any related code. Also, the first line in the 'addedToWorld' method in the SpaceShip class (beginning with 'healthbar.setImage') can be removed.
SAAEngineer SAAEngineer

2014/5/26

#
now there's an image of a green foot , and nowhere else it is mentioned.
danpost danpost

2014/5/26

#
SAAEngineer wrote...
now there's an image of a green foot , and nowhere else it is mentioned.
Make sure there is only one (that there are not any behind it). Then re-post your Spaceship and Healthbar classes.
SAAEngineer SAAEngineer

2014/5/26

#
There are multiple images behind it of green feet.
import greenfoot.*;

public class Spaceship extends CollisionObjects
{
    int hitcounter = 0;
    Actor healthbar = new Actor(){};

    // add healthbar to world and set its initial image
    public void addedToWorld(World world)
    {
        world.addObject(healthbar, 24, 2);
    }

    public void act() 
    {     
        hit();
    }

    public void hit()
    {
        Actor badGuy = getOneIntersectingObject(BadGuy.class);
        if(badGuy != null)
        {
            setImage("broken.jpg");
            Greenfoot.playSound("alert.mp3");
            Damage hit2 = new Damage();
            getWorld().addObject(hit2,30,50);
            getWorld().removeObject(badGuy);
            hitcounter++;
            // update the image of the healthbar
            healthbar.setImage("Healthbar"+hitcounter+".png");
        }
    }
}
import greenfoot.*;

public class Healthbar extends Actor
{
    int imageNum;

    public Healthbar()
    { // set initial image
        setImage("Healthbar0.png");
    }

    public void showHit()
    { // update image
        imageNum++;
        setImage("Healthbar"+imageNum+".png");
    }
}
danpost danpost

2014/5/26

#
Ok. how many spaceships are you adding to your world (are there multiples of it behind the one you see)? and, what other classes are there in your project that extend SpaceShip, if any?
danpost danpost

2014/5/26

#
And still you have not done this --
You did not change line 6 of the Spaceship class to:
Healthbar healthbar = new Healthbar();
nor, did you change line 32 (31 in your last posting) to:
healthbar.showHit();
SAAEngineer SAAEngineer

2014/5/26

#
danpost wrote...
And still you have not done this --
You did not change line 6 of the Spaceship class to:
Healthbar healthbar = new Healthbar();
nor, did you change line 32 (31 in your last posting) to:
healthbar.showHit();
Ow sorry, i did change it but i copied an old file, but still it's the same.
SAAEngineer SAAEngineer

2014/5/26

#
danpost wrote...
Ok. how many spaceships are you adding to your world (are there multiples of it behind the one you see)? and, what other classes are there in your project that extend SpaceShip, if any?
Only one class but it's only an image, i'm adding spaceships in my world with this:
public BackgroundLvL1()
    {    
        // Create a new world with 60x100 cells with a cell size of 8x pixels.
        
        super(60,100,8);
        for (int x = 0; x < getWidth(); x++) {
        addObject(new Spaceship(), x*4,getHeight()-2);
       
                }
        }  
          
danpost danpost

2014/5/26

#
Now I am really confused. For whose health is the healthbar? (evidently not for the spaceships!) How many BagGuy objects are you putting in the world? No. I know it is multiple (but not necessarily at the same time). What I do know is this: you are adding 60 spaceships into a world that is 480x800 in size with cellsize of 8 (a 60x100 gridded world). only 16 of them will be visible across the base of your world; the other 44 will be behind the right-most one (stacked on top of each other). With each spaceship you add, you are also adding (because of where you create and add a healthbar) 60 Healthbar objects. Are the spaceship objects across the bottom of the screen to represent just ONE ship or are they actually 60 different ships?
SAAEngineer SAAEngineer

2014/5/27

#
They have to represent ONE ship!
danpost danpost

2014/5/27

#
Then, in your Spaceship class, change
 Healthbar healthbar = new Healthbar();
// to
static Healthbar healthbar;
then, in your initial world constructor, add the following line:
Spaceship.healthbar = new Healthbar();
and change your addedToWorld method in the Spaceship class to this:
public void addedToWorld(World world)
{
    if (healthbar.getWorld() == null) world.addObject(healthbar, 24, 2);
}
or add it to the world in your game world constructor.
SAAEngineer SAAEngineer

2014/5/29

#
i get <indentifier> expected when i try to add it to my initial world constructor.
danpost danpost

2014/5/29

#
SAAEngineer wrote...
i get <indentifier> expected when i try to add it to my initial world constructor.
Either you added it into your world constructor incorrectly or you did not adjust code elsewhere to compensate for the change in its location.
You need to login to post a reply.
1
2