I have a health bar that I need to change based on when projectiles hit the player. The health bar I got with the help of greenfoot member so its not my personal code so I am only mostly sure I understand how it works.
I want the Ship( player ) to lose health every time a MeanMissile ( enemy projectile) makes contact.
Ship Code-
import greenfoot.*;
import java.awt.Color;
import java.awt.color.*;
public class HealthBar extends Actor
{
private final int maxValue = 200; // whatever max value you desire
private int barValue = 200;
private int lastValue = 0;
public HealthBar()
{
}
public void act()
{
if (barValue != lastValue)
{
lastValue = barValue;
int pctHealth = (int) (200 * barValue / maxValue); // 200 is maxPixelHealth
// Create thermometer -- sorta speak
GreenfootImage imgOne = new GreenfootImage(206,25);
imgOne.setColor(Color.CYAN);
imgOne.fill();
imgOne.setColor(Color.BLUE);
imgOne.drawRect(2,2,202,21);
// Add mercury, if there is any temperature -- sorta speak
if (pctHealth != 0)
{
GreenfootImage imgTwo = new GreenfootImage(pctHealth,19);
imgTwo.setColor(Color.RED);
imgTwo.fill(); // Completes the second image
imgOne.drawImage(imgTwo,3,3); // Puts mercury into the thermometer
}
// imgOne.scale(myX, myY); // Dimensions myX and myY are whatever size you wish to make it
imgOne.setTransparency(128); // Adjust value as wanted or delete statement for no transparency
setImage(imgOne);
}
}
public void chgHealth(int chgValue)
{
barValue += chgValue;
if (barValue > maxValue) barValue = maxValue;
if (barValue < 0) barValue = 0;
}
public int getHealth()
{
return barValue;
}
}import greenfoot.*;
/**
* An demo class which is meant to be a camera follower.
* It moves to face your mouse cursor, and it can move
* back and forward.
*
* @author Sven van Nigtevecht
* @version 1.0
*/
public class Bug extends ScrollActor
{
/** The number of cells we move forward and backword */
private static final int MOVE_AMOUNT = 5;
private int shootingCounter;
public int angle;
public int barValue;
/**
* Move to face the mouse,
* and listen to the up and down keys.
*/
public void act()
{
MouseInfo m = Greenfoot.getMouseInfo();
getWorld().setCameraDirection(getRotation());
Actor First = null;
if (Greenfoot.isKeyDown("down"))
{
// move the camera backwards:
getWorld().moveCamera(-MOVE_AMOUNT);
}
if (Greenfoot.isKeyDown("up"))
{
// move the camera forwards:
getWorld().moveCamera(MOVE_AMOUNT);
}
if (Greenfoot.isKeyDown("left"))
{
turn ( -3 );
angle -= 3;
}
if (Greenfoot.isKeyDown("right"))
{
turn ( 3 );
angle += 3;
}
if (Greenfoot.isKeyDown("b"))
{
getWorld().moveCamera( MOVE_AMOUNT * 2) ;
}
if (Greenfoot.isKeyDown("b"))
{
getWorld().moveCamera( MOVE_AMOUNT * 2) ;
}
if (First != null )
{
move (-10);
}
if ( angle > 360)
{
angle = 0;
}
if ( angle < 0)
{
angle = 360;
}
shoot();
Actor MeanMissile;
MeanMissile = getOneObjectAtOffset(0,0, MeanMissile.class);
if ( MeanMissile != null )
{
World Galaxy;
Galaxy = getWorld();
Galaxy.removeObject(MeanMissile);
Galaxy.addObject( new explosion(), getGlobalX(), getGlobalY());
}
}
public void shoot()
{
shootingCounter--;
if(Greenfoot.isKeyDown("s") && shootingCounter <= 0 )
{
shootingCounter=20;
getWorld().addObject(new ShipMissile(angle), getGlobalX(), getGlobalY());
}
}
public int getGlobalY()
{
if (world == null)
throw new IllegalStateException("Actor not in world. Either is hasn't"+
" been inserted, or it has been deleted.");
return globalY;
}
public int getGlobalX()
{
if (world == null)
throw new IllegalStateException("Actor not in world. Either is hasn't"+
" been inserted, or it has been deleted.");
return globalX;
}
}
