Here is my code for a helicopter. The main method I'm worried about is heliDie(). In here, my idea was to make it so that the helicopter would die after 2 shots. But, instead of dying after 2 shots, it might be 3, or 7, or 8 shots.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Helicopter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Helicopter extends Actor
{
boolean kill = true;
public int heliHealth = 2;
private int timer;
boolean time = false;
private int shoot;
private int imageNumber = 1;
private int count;
GreenfootImage image = new GreenfootImage("r1.png");
/**
* Act - do whatever the Helicopter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setLocation(getX() -2, getY());
setImage();
shootBullet();
heliDie();
if (kill)
{
if (isAtEdge())
{
getWorld().removeObject(this);
}
}
}
public void setImage()
{
image = new GreenfootImage("r" + imageNumber + ".png");
if (count % 10 == 0)
imageNumber++;
if (imageNumber == 4)
{
imageNumber = 1;
}
{
setImage(image);
}
count++;
}
public void shootBullet()
{
shoot++;
if (shoot == 100)
{
getWorld().addObject(new Bullet(), getX() -100, getY());
Greenfoot.playSound("Gunshot.wav");
shoot = 0;
}
}
public void heliDie()
{
if (kill)
{
if (isTouching(Tracking_Missile.class))
{
heliHealth-=1;
kill = false;
time = true;
MyWorld world = (MyWorld)getWorld();
//world.addScore(3);
removeTouching(Tracking_Missile.class);
}
}
if (time)
{
timer++;
}
if (timer == 5)
{
kill = true;
time = false;
}
if (heliHealth == 0)
{
getWorld().addObject(new Death(), getX(), getY());
getWorld().removeObject(this);
}
}
}

