Hi! I'm having trouble with adding health to my enemy units and decreasing it when it hits the bullet. I'm using the regular ghost that will die in 1 hit as the parent to the bossGhost that will die in 5 hit and tried to set the health of the bossGhost to 5 using setter and getter. I don't know if I'm doing it right or if I need to do something else.
Here is the code for my ghost.
Here is the code to my evilBoss with 5 health.
Here is my bullet code.
Thank you very much :D.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class ghost here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ghost extends Actor
{
/**
* Act - do whatever the ghost wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
protected int ghostSpeed = 1;
public int ghostHealth = 1;
public void act()
{
moveAuto(ghostSpeed);
hitBorder();
}
public int currGhostHealth()
{
return ghostHealth;
}
public void setGhostHealth(int Health)
{
this.ghostHealth = ghostHealth;
}
public void minGhostHealth(int minHealth)
{
this.ghostHealth = ghostHealth - minHealth;
}
public void moveAuto(int ghostSpeed)
{
setLocation(getX(), getY() + ghostSpeed);
}
public int hitCount;
public void hitBorder()
{
Actor border = getOneIntersectingObject(houseBorder.class);
if(border != null)
{
World myWorld = getWorld();
myWorld.removeObject(this);
Greenfoot.delay(5);
Greenfoot.setWorld(new GameOverScreen());
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class evilGhost here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class evilGhost extends ghost
{
/**
* Act - do whatever the evilGhost wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveAuto(ghostSpeed);
evilGhost1();
}
public void evilGhost1()
{
setGhostHealth(5);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends Actor
{
/**
* Act - do whatever the Bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int maxDistanceTravelled = 100;
//private int speed = 5;
public void act()
{
move(4);
destroyEnemies();
checkBoundaries();
bulletRange();
}
//we add a method "checkBoundaries()" that destroys bullets that are off screen.
public void checkBoundaries()
{
if (getWorld() == null) return;
World myWorld = getWorld();
if(this != null){
if(getX() > myWorld.getWidth() - 1)
myWorld.removeObject(this);
else if(getX() < 1)
myWorld.removeObject(this);
else if(getY() > myWorld.getHeight() - 1)
myWorld.removeObject(this);
else if(getY() < 1)
myWorld.removeObject(this);
}
}
//"destroyEnemies()" destroys enemies.
public void destroyEnemies()
{
//"Enemy" can be any class that you want the bullet to destroy.
if (getWorld() == null) return;
if(this != null)
{
Actor ghost = getOneIntersectingObject(ghost.class);
if(ghost != null)
{
World myWorld = getWorld();
//myWorld.removeObject(ghost);
ghost Ghost = new ghost();
Ghost.minGhostHealth(1);
if(Ghost.currGhostHealth() == 0)
{
MyWorld myworld = (MyWorld)myWorld;
Score score = myworld.getScore();
score.addScore();
myWorld.removeObject(ghost);
myWorld.removeObject(this);
}
}
}
}
public void bulletRange(){
if (getWorld() == null) return;
maxDistanceTravelled -= 1;
if(maxDistanceTravelled == 0)
{
getWorld().removeObject(this);
}
}
}

