So I'm writing a little game where you fly around in a biplane and drop bombs on bad guys, in this case turrets. However I've run into a little problem. At first, before I wrote the code to remove the bomb on impact, it would go right through the turret, instantly killing it (I want it to take a couple hits, and while passing through the target of course it's hitting multiple times) That's nothing I'm not familiar with. The problem is this, now that I have written code to remove the bomb on impact, apparently it's removing it prior to the health of the turret getting deducted, so I can drop bombs on the turret all day long, and nothing happens to it.
Here's the bomb code:
And here's the turret code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bomb here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bomb extends Actor
{
/**
* Act - do whatever the bomb wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(4);
if(isTouching(turret.class))
{
World myWorld = (getWorld());
myWorld.removeObject(this);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class turret here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class turret extends Actor
{
private int health = 2;
/**
* Act - do whatever the zeppelin wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Actor bomb=getOneIntersectingObject(bomb.class);
{
if (bomb !=null)
{
health--;
}
{
if (health==0)
die();
}
}
}
public void die()
{
World myWorld = (getWorld());
myWorld.removeObject(this);
}
}
