i am making a game and got pretty far with it so far but the health system doesn't seem to work! i have 6 health point so meaning i can be hit 6 times without dying and i have it set when i hit an enemy health int drops 1 and if health == 5 it removes heart number 6 but it doesn't remove the hearts throughout the whole cycle! after 6 hits it displays that i have lost but never removes the hearts! here is my code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MainPlayer here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Players
{
//images for animation
GreenfootImage WalkBack = new GreenfootImage("WalkBack.png");
GreenfootImage WalkDown = new GreenfootImage("WalkDown.png");
GreenfootImage WalkLeft = new GreenfootImage("WalkLeft.png");
private boolean spaceDown;
private boolean ZDown;
public int health = 6;
public void act()
{
move();
attack1();
//attack2();
countGems();
die();
removeHearts();
//act method does whatever the Actor wants to do. This method is called whenever the
//act or run button gets pressed in the enviroment
}
public void move()
{
int x = getX();
int y = getY();
//moving
if(Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-2);
setImage(WalkBack);
if ( isTouching(Walls.class) )
{
setLocation(getX(), getY()+2);
//checks walls
}
if(canSee(Enemy1.class))
{
setLocation(getX(), getY()+10);
removeHearts();
health--;
}
}
if(Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+2);
setImage(WalkDown);
if ( isTouching(Walls.class) )
{
setLocation(getX(), getY()-2);
//checks walls
}
if(canSee(Enemy1.class))
{
setLocation(getX(), getY()-10);
removeHearts();
health--;
}
}
if(Greenfoot.isKeyDown("left"))
{
setLocation(getX()-2, getY());
setImage(WalkLeft);
if ( isTouching(Walls.class) )
{
setLocation(getX()+2, getY());
//checks walls
}
if(canSee(Enemy1.class))
{
setLocation(getX()+10, getY());
removeHearts();
health--;
}
}
if(Greenfoot.isKeyDown("right"))
{
setLocation(getX()+2, getY());
//setImage("NAME");
if ( isTouching(Walls.class) )
{
setLocation(getX()-2, getY());
//checks walls
}
if(canSee(Enemy1.class))
{
setLocation(getX()-10, getY());
removeHearts();
health = health - 1;
}
}
//moving
}
public void attack1()
{
//attacking the enemy
if (spaceDown != Greenfoot.isKeyDown("space"))
{
spaceDown = ! spaceDown;
if (spaceDown)
{
Attack1 attack1 = new Attack1();
getWorld().addObject(attack1, getX()+1, getY());
attack1.setRotation(getRotation());
}
}
}
public void die()
{
if(health <= 0)
{
Lose lose = new Lose();
eat(Heart.class);
getWorld().addObject(lose, 140, 140);
}
}
public void removeHearts()
{
if(health == 5)
{
eat(Heart6.class);
}else if(health == 4)
{
eat(Heart5.class);
}else if(health == 3)
{
eat(Heart4.class);
}else if(health == 2)
{
eat(Heart3.class);
}else if(health == 1)
{
eat(Heart2.class);
}else if(health <= 0)
{
die();
}
}
public void addGems()
{
if(canSee(Gems.class))
{
eat(Gems.class);
GemCounter++;
}
}
}
