I'm making a fighting game for a school project. There are two characters. a monkeyWizard (throws fireballs) and a wolfWarrior (throws knifes). I'm using the isTouching method to make the weapons (fireball and knife) dissappear with the remove method and I'm using isTouching in the classes of the characters to detect if it is touching a knife or fireball. I think it is because the game removes the weapons before the characters can detect it. I'm including the monkeyWizard class and Knife class. When i run my game the knife will disappear but the monkeyWizard's health does not decrease.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class knife_L here.
*
* @author (Alex J)
* @version (April 2017)
*/
public class knife_L extends weapons
{
private GreenfootImage KL;
public knife_L() // left going knife
{
KL = new GreenfootImage("knife_L.png");
}
public void act()
{
setImage(KL);
setLocation(getX() -20, getY());
if (isTouching(monkeyWizard.class))
{
getWorld().removeObject(this);
}
else if (isAtEdge())
{
getWorld().removeObject(this);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class monkeyWizard here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class monkeyWizard extends characters
{
private boolean onGround;
private boolean isRight = true;
private boolean oneDown;
private int health = 100;
private int timer = 0;
private GreenfootImage wizRight;
private GreenfootImage wizLeft;
private GreenfootImage wizARight; //wiz attack left
private GreenfootImage wizALeft;
public monkeyWizard()
{
wizRight = new GreenfootImage("mk_R.png");
wizLeft = new GreenfootImage("mk_L.png");
wizARight = new GreenfootImage("mk_R_A.png");
wizALeft = new GreenfootImage("mk_L_A.png");
}
public void act()
{
checkKeyPress();
checkOnGround();
displayMHealth();
hit();
}
public void setIsRight(boolean a)
{
isRight = a;
}
public void checkKeyPress()
{
if (Greenfoot.isKeyDown("d"))
{
setImage(wizRight);
setIsRight(true);
if (getX() < 830)
move(10);
}
if (Greenfoot.isKeyDown("a"))
{
setImage(wizLeft);
setIsRight(false);
if (getX() > 120)
move(-10);
}
if ("w".equals(Greenfoot.getKey()))
{
if (checkOnGround())
{
setLocation(getX(), getY() - getGravity());
}
}
if(timer>0)
timer--;
else if(timer==0)
{
if (Greenfoot.isKeyDown("s")) //shoot
{
if (isRight)
{
setImage(wizARight);
throwFB();
timer=20;
}
else if(!isRight)
{
setImage(wizALeft);
throwFB();
timer=20;
}
}
}
}
public void throwFB()
{
if (!isRight)
{
getWorld().addObject( new fireball_L(), getX(), getY());
}
else if (isRight)
{
getWorld().addObject( new fireball_R(), getX(), getY());
}
}
public boolean checkOnGround()
{
if ( getY() == 500)
{
return true;
}
else
return false;
}
public void hit()
{
if (isTouching(knife_L.class) || isTouching(knife_R.class))
{
Greenfoot.playSound("ow.wav");
health -= 10;
}
}
public int getHealth()
{
return health;
}
public void displayMHealth()
{
getWorld().showText("Monkey Wizard health: "+ getHealth(),150, 50);
}
}