SAAEngineer wrote...
It extends Actor
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");
}
}Healthbar healthbar = new Healthbar();
healthbar.showHit();
Healthbar healthbar = new Healthbar();
healthbar.showHit();
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);
}
}
Healthbar healthbar = new Healthbar(); // to static Healthbar healthbar;
Spaceship.healthbar = new Healthbar();
public void addedToWorld(World world)
{
if (healthbar.getWorld() == null) world.addObject(healthbar, 24, 2);
}