hello, I have made a space shooting game. You are a Cruiser and you shoot the Pirates. I wanted to make a "pet" to help me shoot the Pirates. It follows the pirate and shoots at it, but when it's gone, an error pops up. I think it is because the pet is looking for a pirate but can't find it, which makes an error. The crazy thing is, is that when a Pirate dies, another spawns right away. Please help me fix it. Here is the "Pet's" code:
Here is the Pirate's code:
P.S. the "pet" is actually called "Cruiser2".
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Cruiser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Cruiser2 extends Actor
{
private int gunHeat = 0;
Pirate pirate;
Cruiser2 cruiser2;
/**
* Act - do whatever the Cruiser wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
motionControl();
fire();
gunHeat--;
isHit();
}
public Cruiser2(Pirate pirate)
{
this.pirate = pirate;
GreenfootImage image = getImage();
image.scale(100, 50);
}
public void fire()
{
if (gunHeat <= 0)
{
getWorld().addObject(new Laser2(), getX()+40, getY()+10);
getWorld().addObject(new Laser2(), getX()+50, getY());
Greenfoot.playSound("pew.mp3");
gunHeat = 15;
}
}
public void isHit()
{
Actor missile = getOneIntersectingObject(Missile.class);
if (missile != null)
{
getWorld().removeObject(missile);
getWorld().removeObject(this);
}
}
private void motionControl()
{
this.pirate = pirate;
this.cruiser2 = cruiser2;
if (pirate != null)
{
if (pirate.getY() > getY()+5)
{
setLocation(getX(), getY()+5);
}
if (pirate.getY() < getY()-5)
{
setLocation(getX(), getY()-5);
}
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.*;
/**
* Write a description of class Pirate here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Pirate extends Actor
{
private static int count;
private int lives = 5;
private int gunHeat = 0;
Cruiser cruiser;
Pirate pirate;
public Pirate()
{
count = 0;
lives = 5;
}
/**
* Act - do whatever the Pirate wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
gunHeat--;
if (getX() <= 0)
{
setLocation(getWorld().getWidth() + 40, Greenfoot.getRandomNumber(600));
}
fireShot(cruiser);
isHit();
isKilled();
GreenfootImage image = getImage();
}
private void move()
{
setLocation(getX() - 3, getY());
}
private boolean inRange()
{
Space world = (Space) getWorld();
Cruiser cruiser = world.getCruiser();
int top = Math.abs(cruiser.getY()-getY());
int bottom = Math.abs(getY() - cruiser.getY());
return(top < 50 && bottom < 50);
}
public void fireShot(Cruiser cruiser)
{
if (inRange() && (gunHeat <= 0) && (Greenfoot.getRandomNumber(20) < 5))
{
getWorld().addObject(new Missile(), getX(), getY());
Greenfoot.playSound("missile.mp3");
gunHeat = 15;
}
}
public void isHit()
{
Actor enemylaser = getOneIntersectingObject(EnemyLaser.class);
if (enemylaser != null)
{
getWorld().removeObject(enemylaser);
count++;
//System.out.println("count in isHit is: " + count);
addPoints();
getWorld().addObject(new Explosion(), getX(), getY());
Greenfoot.playSound("explode.mp3");
getWorld().addObject(new Pirate(), 950, (Greenfoot.getRandomNumber(600)));
if (Greenfoot.getRandomNumber(1000) < 30)
{
getWorld().addObject(new PowerPod(), getX(), getY());
}
getWorld().removeObject(this);
}
}
public void isKilled()
{
Actor laser2 = getOneIntersectingObject(Laser2.class);
if (laser2 != null)
{
getWorld().removeObject(laser2);
count++;
//System.out.println("count in isHit is: " + count);
addPoints();
getWorld().addObject(new Explosion(), getX(), getY());
Greenfoot.playSound("explode.mp3");
getWorld().addObject(new Pirate(), 950, (Greenfoot.getRandomNumber(600)));
if (Greenfoot.getRandomNumber(1000) < 30)
{
getWorld().addObject(new PowerPod(), getX(), getY());
}
getWorld().removeObject(this);
}
}
public void addPoints()
{
Space space = (Space) getWorld();
Score score = space.getScore();
//System.out.println("I scored." + count);
score.updateScore("Score: " + count, score.getPower(), score.getLives());
}
}

