I'm trying to make a health bar with 180 separate sprites to make up 180 total health. I'm trying to do this with setImage. How would I do this? (or is there a more efficient way?) Thanks.
setImage("Health"+(health+1)+".png");public void act()
{
takeDamage();
}
public void takeDamage()
{
if (lives == 9)
{
setImage("Health9.png");
}
if (lives == 8)
{
setImage("Health8.png");
}
}public class Boss extends Actor
{
private int health = 180;
private BossHealth bossHealth = new BossHealth(); // creates the health bar for this boss
protected void addedToWorld(World world)
{
positionHealthbar(); // adds bar into world
}
/** adds healthbar to world, if needed, and updates its position */
private void positionHealthbar()
{
getWorld().addObject(bossHealth, 0, 0);
bossHealth.setLocation(getX(), getY()-30);
}
/** class of healthbar */
private class BossHealth extends Actor
{
public BossHealth()
{
updateImage(); // initializes image
}
/** updates image of healthbar */
protected void updateImage()
{
setImage(new GreenfootImage("Health"+health+".png"));
}
}
}health--;
if (health == 0)
{
getWorld().removeObject(bossHealth);
getWorld().removeObject(this);
return;
}
bossHealth().updateImage();import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Boss here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Boss extends obstacles
{
private int health = 63;
private BossHealth bossHealth = new BossHealth();
public int BossX;
public int BossY;
protected void addedToWorld(World bossworld)
{
positionHealthBar();
}
/**
* 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()
{
Plane2 plane2 = (Plane2)getWorld().getObjects(Plane2.class).get(0);
turnTowards(plane2.getX(), plane2.getY());
Boss boss = new Boss();
move(6);
BossX = getX();
BossY = getY();
if (isTouching(Bomb.class))
{
health--;
}
if (health == 0 )
{
getWorld().removeObject(this);
return;
}
}
private void positionHealthBar()
{
getWorld().addObject(bossHealth, 0, 0);
bossHealth.setLocation(getX(), getY()-30);
}
private class BossHealth extends Actor
{
public BossHealth()
{
updateImage(); // initializes image
}
/** updates image of healthbar */
protected void updateImage()
{
setImage(new GreenfootImage("Health"+health+".png"));
}
}
}
positionHealthBar();
getWorld().removeObject(bossHealth);
bossHealth.updateImage();