I have been trying to create a health bar where when the player controlled character (MainCharacter) is hit by an enemy (Flame) a third class which contains a health bar image changes to accommodate for the damage. I am a bit stuck when it comes to 'sharing' the variables. I have a variable called "health" and when there is a collision between the enemy and the main character, it should become one less. This should then take effect in the Health bar class and its image be changed. Right now there are no syntax errors but it appears as though the health variable is not actually being affected.
Here is my code for the Main Character:
And for the enemy class:
And for the Health Bar Class:
Thankyou so much for your help in advance :D
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.Timer; /** * Write a description of class MainCharacter here. * * @author (your name) * @version (a version number or a date) */ public class MainCharacter extends Actor { /** * Act - do whatever the MainCharacter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkFall(); walk(); rebound(); } private int speed = 4; // running speed (sideways) private int vSpeed = 0; //velocity speed private int acceleration = 2; private int jumpStrength = 23;//jump height public void walk(){ if (onGround() && Greenfoot.isKeyDown("w")){ //checks the character is on the ground jump();//runs jump } if (Greenfoot.isKeyDown("d")){//checks if the right key is being pressed moveRight();//walks the character to the right } if (Greenfoot.isKeyDown("a")){//checks if the a key is being pressed moveLeft();//walks the character to the left } } public void jump() { vSpeed = -jumpStrength; //takes the jump strenght from the v speed (negative fall which makes jump) fall();//makes the character fall after it has jumped } public void checkFall() { if(onGround()) { vSpeed = 0; //sets velocity to 0 (not falling) when the character is on the ground } else { fall(); } } public boolean onGround() { int myHeight = getImage().getHeight(); Actor under = getOneObjectAtOffset(0, myHeight/2, Board.class); return under != null; } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; //adds acceleration to the vSpeed which is 0 (makes positive velocity which makes fall) } public void moveRight() { setImage("ppl2Right.png"); setLocation ( getX() + speed, getY() ); //adjusts movment based on speed set } public void moveLeft() { setImage("ppl2.png"); setLocation ( getX() - speed, getY() );//adjusts movment based on speed set } public void rebound() { Actor flame = getOneIntersectingObject(Flame.class); Health health = new Health(); if(flame== null) { //do nothing because not intersecting } else if(onGround()) { bounce(); //runs the rebound health.setHealth(-1); } else { health.setHealth(-1); } } public void bounce() { setLocation ( getX() + 90, getY() -5); setLocation (getX() + 3, getY() ); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Flame here. * * @author (your name) * @version (a version number or a date) */ public class Flame extends Actor { /** * Act - do whatever the Flame wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // moveAround(); move(); } private int a = 1; private boolean swi = true; private void move(){ moveRight(); moveLeft(); } private void moveRight(){ if(swi == true){ setLocation ( getX() + 1, getY() ); Greenfoot.delay(1); a++; if(a>350){ swi = false; } } } private void moveLeft(){ if(swi == false){ setLocation ( getX() - 1, getY() ); Greenfoot.delay(1); a--; if(a<1){ swi = true; } } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Health here. * * @author (your name) * @version (a version number or a date) */ public class Health extends Actor { /** * Act - do whatever the Health wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { healthStatus(); } private int health = 4; public void setHealth(int points) { health = health + points; } public int getHealth() { return health; } public void healthStatus() { switch (health) { case 1: setImage("health25.png"); break; case 2: setImage("health50.png"); break; case 3: setImage("health75.png"); break; case 4: setImage("healthMax.png"); break; default:break; } } }