In my game your supposed to dodge Thwomps and Terence from above. When hitting them you take damage. However i want when hitting Watermelon that you gain 5 health and that it is visible in the healthbar. Here is my code for my player character and healthbar. Im a student doing this for school and help is aprreciated.
import greenfoot.*;
public class Joker extends Actor {
public int snelheid = 4;
private int vSpeed = 0;
private int acceleration = 1;
private int state = 1;
int sprinten = 1;
int tellerAnimatie = 10;
private Healthbar healthBar;
private SimpleTimer immunityTimer = new SimpleTimer();
private GreenfootSound sterSound = new GreenfootSound("Super Mario Bros - Star.mp3");
private int thwompCooldown = 0;
private final int THWOMP_COOLDOWN_DURATION = 70;
public Joker(Healthbar healthBar) {
this.healthBar = healthBar;
}
public void act() {
checkKeys();
fall();
if (state == 2 && immunityTimer.millisElapsed() > 15000) {
state = 1;
}
if (thwompCooldown > 0) {
thwompCooldown--;
}
raakThwomp();
raakSuperStar();
}
public void raakSuperStar() {
if (isTouching(SuperStar.class)) {
state = 2;
immunityTimer.mark();
sterSound.play();
}
}
public void raakThwomp() {
if (state == 1 && thwompCooldown == 0 && (isTouching(Thwomp.class) || isTouching(SuperThwomp.class))) {
int damage = 4;
if (isTouching(SuperThwomp.class)) {
damage = 7;
}
if (isTouching(SuperThwomp.class)) {
damage = 7;
}
if (isTouching(Terence.class)) {
damage = 25;
}
if (healthBar != null) {
healthBar.loseHealth(damage);
thwompCooldown = THWOMP_COOLDOWN_DURATION;
Greenfoot.playSound("Joker Persona 5 Royal Sound Effects HD.mp3");
Actor thwomp = getOneIntersectingObject(Thwomp.class);
Actor superThwomp = getOneIntersectingObject(SuperThwomp.class);
Actor Terence = getOneIntersectingObject(Terence.class);
if (thwomp != null) {
getWorld().removeObject(thwomp);
}
if (superThwomp != null) {
getWorld().removeObject(superThwomp);
}
if (Terence != null) {
getWorld().removeObject(Terence);
}
}
}
}
public void checkKeys() {
if (Greenfoot.isKeyDown("d")) {
if (sprinten<=10)
{
if(tellerAnimatie == 10) {
setImage("jokerright" + sprinten + ".png");
sprinten++;
tellerAnimatie = 1;
}
}
else
{
sprinten=1;
}
tellerAnimatie++;
setLocation(getX() + snelheid, getY());
}
if (Greenfoot.isKeyDown("a")) {
if (sprinten<=10)
{
if(tellerAnimatie == 10) {
setImage("jokerleft" + sprinten + ".png");
sprinten++;
tellerAnimatie = 1;
}
}
else
{
sprinten=1;
}
tellerAnimatie++;
setLocation(getX() - snelheid, getY());
}
if (Greenfoot.isKeyDown("w")&& (onGrond() == true))
{
jump();
}
}
public void jump() {
if (getWorld().getClass() == Hell.class || getWorld().getClass() == Japan.class) {
vSpeed = -20;
} else {
vSpeed = -25;
}
fall();
setImage("jokerjump6.png");
}
public boolean onGrond()
{
Actor under = getOneObjectAtOffset ( 0, getImage().getHeight() /2, Grond.class);
return under != null;
}
public void fall() {
vSpeed = vSpeed + acceleration;
setLocation(getX(), getY() + vSpeed);
}
}/**
* Write a description of class Healthbar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Healthbar extends Actor {
int health = 40;
int maxHealth = 40;
int healthBarWidth = 120;
int healthBarHeight = 20;
int pixelsPerHealthPoint = healthBarWidth / health;
public Healthbar() {
update();
}
public void act() {
update();
}
public void update() {
GreenfootImage image = new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2);
image.setColor(Color.WHITE);
image.fillRect(0, 0, healthBarWidth + 2, healthBarHeight + 2);
if (health <= 0) {
Greenfoot.setWorld(new GameOverScherm());
} else {
if (health < 10) {
image.setColor(Color.RED);
} else {
image.setColor(Color.GREEN);
}
image.fillRect(1, 1, health * pixelsPerHealthPoint, healthBarHeight);
}
setImage(image);
}
public void loseHealth(int amount) {
health -= amount;
if (health < 0) {
health = 0;
}
}
public void add(int amount) {
health += amount;
if (health > maxHealth) {
health = maxHealth;
}
}
}
