I've found a way how my Chased/my character would get damaged by his ammo/fire which is his own subclass. The problem is, when he first hits it, the healthbar decreases normally. But the second time he hits it, the healthbar decreases rapidly, what code do I need to add or edit?
Here is the code for the Character:
Here is the code for the Healthbar:
import greenfoot.*;
import java.awt.Color;
/**
* Class Chased (subclass of Actor): a user-controlled object with a mouse-controlled gun
*/
public class Chased extends Actor
{
boolean touchingZ = false;
boolean touchingZ2 = false;
private int rotation;
boolean touchingFire = false;
Gun gun = new Gun(); // the gun for this actor
Chaser chaser = new Chaser();
Runner runner = new Runner();
int timer;
int bullet=10;
/**
* creates image for actor
*/
public Chased()
{
// the image for this actor
GreenfootImage image = new GreenfootImage(50, 50);
image.fillOval(0, 0, 50, 50);
image.setColor(Color.green);
image.fillOval(7, 7, 36, 36);
setImage(image);
}
/**
* adds the gun for this actor into the world
*
* @param world the world this actor was added into
*/
public void addedToWorld(World world)
{
world.addObject(gun, getX(), getY()); // add the gun belonging to this actor into the world
}
/**
* move user-controlled actor and its gun; also, check for game over
*/
public void act()
{
// user-controlled movement
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("a")) dx--;
if (Greenfoot.isKeyDown("d")) dx++;
if (Greenfoot.isKeyDown("w")) dy--;
if (Greenfoot.isKeyDown("s")) dy++;
setLocation(getX()+1*dx, getY()+1*dy);
// limit to world bounds
if (getX() < 25) setLocation(25, getY());
if (getY() < 25) setLocation(getX(), 25);
if (getX() > getWorld().getWidth()-25) setLocation(getWorld().getWidth()-25, getY());
if (getY() > getWorld().getHeight()-25) setLocation(getX(), getWorld().getHeight()-25);
// position gun
gun.setLocation(getX(), getY());
// check game over
Actor chaser = getOneIntersectingObject(Chaser.class);
if (chaser != null)
{
World world = getWorld();
Welt welt = (Welt)world;
HealthBar healthbar = welt.getHealthBar();
Numberz numberz = welt.getNumberz();
if(touchingZ == false)
{
healthbar.loseHealth();
touchingZ = true;
if(healthbar.health <=0)
{
world.removeObjects(world.getObjects(null));
Greenfoot.setWorld(new Stats(((Welt)world).score));
}
}
}else{
touchingZ = false;
}
Actor runner = getOneIntersectingObject(Runner.class);
if (runner != null)
{
World world = getWorld();
Welt welt = (Welt)world;
HealthBar healthbar = welt.getHealthBar();
Numberz numberz = welt.getNumberz();
if(touchingZ2 == false)
{
healthbar.loseHealth();
touchingZ2 = true;
if(healthbar.health <=0)
{
world.removeObjects(world.getObjects(null));
Greenfoot.setWorld(new Stats(((Welt)world).score));
}
}
}else{
touchingZ2 = false;
}
}
/**
* Class: Gun (subclass of Actor -- inner class of Chased): the gun for this actor
*/
private class Gun extends Actor
{
/**
* creates image for actor
*/
public Gun()
{
// the image for this actor
GreenfootImage image = new GreenfootImage("Picture1.png");
setImage(image);
}
public boolean Munition(){
if(bullet>0){
return true;
}
else{
return false;
}
}
private boolean mouseclick;
/**
* responds to mouse movement and mouse button clicks
*/
public void act()
{
// turn towards mouse when mouse moves
if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
{
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null) turnTowards(mouse.getX(), mouse.getY());
}
// detect mouse clicks to fire shot
World world = getWorld();
Welt welt = (Welt)world;
AmmoBar ammobar = welt.getAmmoBar();
if(!mouseclick && (Greenfoot.mouseClicked(null)))
{
if(Munition()==true){
mouseclick=true;
getWorld().addObject(new Shot(), getX(), getY());
bullet--;
ammobar.loseAmmo();
if (bullet ==0){
timer = 2000;
}
}
}
if(mouseclick&& !Greenfoot.isKeyDown("space"))
{
mouseclick=false;
}
if (timer > 0){
timer--;
if (timer==0){
bullet+=10;
}
}
}
/**
* Class Shot (subclass of QActor -- inner class of Chased.Gun): the shots from this gun
*/
private class Shot extends QActor
{
/**
* sets bounds fields and creates the image for this actor
*/
public Shot()
{
setBoundedAction(QActor.REMOVE, 5); // set bounds fields
// create image for this actor
GreenfootImage image = new GreenfootImage("FIRE.png");
setImage(image);
}
/**
* initializes rotation and position of actor
*
* @param world the world this actor was added into
*/
public void addedToWorld(World world)
{
setRotation(Gun.this.getRotation()); // set rotation (to that of gun)
move(7000); // set position (at end of gun)
rotation = getRotation();
setRotation(0);
}
/**
* moves actor and checks for removal of actor
*/
public void act()
{
setVX(0);
setVY(0);
addForce(0, getRotation()*QVAL);
move(); // moving (equivalent to 'move(5)' for a non-QActor)
World world = getWorld();
Welt welt = (Welt)world;
Numberz numberz = welt.getNumberz();
HealthBar healthbar = welt.getHealthBar();
Actor chased = getOneIntersectingObject(Chased.class);
if (hits(Chaser.class) || atWorldEdge())
{
getWorld().removeObject(this); // removing
numberz.loseNum();
}else if (hits(Runner.class) || atWorldEdge())
{
numberz.addNum();
getWorld().removeObject(this); // removing
numberz.loseNum();
} else if (chased != null){
if(touchingFire == false)
{
healthbar.loseHealth();
touchingFire = true;
if(healthbar.health <=0)
{
world.removeObjects(world.getObjects(null));
Greenfoot.setWorld(new Stats(((Welt)world).score));
}
}
}else{
touchingFire = false;
}
}
/**
* internal method to detect object collision; returns true if collision detected, else false
*
* @param cls the class of object to check for collision with
* @return a flag indicating whether an object of given class was detected or not
*/
private boolean hits(Class cls)
{
// get intersecting object and return result
Actor clsActor = getOneIntersectingObject(cls);
if (clsActor != null)
{
// remove intersector and bump score
getWorld().removeObject(clsActor);
return true;
}
return false;
}
/**
* internal method that returns a flag indicating world edge encroachment
*
* @return a flag indicating whether the actor has encroached on a world edge or not
*/
private boolean atWorldEdge()
{
// return state of encroachment on world edge
return getX() <= 0 || getY() <= 0 ||
getX() >= getWorld().getWidth()-1 ||
getY() >= getWorld().getHeight()-1;
}
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class HealthBar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HealthBar extends Actor
{
int health = 80;
int hbw = 80;
int hbh = 15;
int pixelsPerHealthPoint = (int)hbw/health;
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public HealthBar()
{
update();
}
public void act()
{
update();
}
public void update()
{
setImage(new GreenfootImage(hbw+2, hbh +2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.WHITE);
myImage.drawRect(0,0,hbw+1,hbh+1);
myImage.setColor(Color.RED);
myImage.fillRect(1,1,health*pixelsPerHealthPoint, hbh);
}
public void loseHealth()
{
health--;
}
public void fireloseHealth()
{
health=-2;
}
}

