This site requires JavaScript, please enable it in your browser!
Greenfoot back
Octavius
Octavius wrote ...

2016/7/8

Galactic Patrol

Octavius Octavius

2016/7/8

#
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:
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);
            }
        }
    }

}
Here is the Pirate's code:
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());
    }
}
P.S. the "pet" is actually called "Cruiser2".
danpost danpost

2016/7/8

#
Octavius wrote...
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. < Code Omitted > P.S. the "pet" is actually called "Cruiser2".
It may seem crazy ... until you realize that the Pirate object stored in the field (which is the dead pirate) is no longer the Pirate object in the world (newly spawned pirate). In other words, even though a new pirate is in the world, any code referencing 'pirate' is still referring to the removed dead one. It would be better if you just removed the objects in the specific classes and not spawn new ones there. You can have the world check for the existence of the objects and add any new one when needed. The following is an example for re-spawning a Pirate object:
// in world act method
if (getObjects(Pirate.class).isEmpty() && !gameOver) addObject(new Pirate(), 950, (Greenfoot.getRandomNumber(600));
The '!gameOver' part I threw in to show that you should ensure the game is not over yet. It should actually be coded in the following manner (if not dealt with in another way):
if (!gameOver)
{
    if (getObjects(Pirate.class).isEmpty()) addObject(new Pirate(), 950, (Greenfoot.getRandomNumber(600));
}
else
{
    // game over code here
    Greenfoot.stop();
}
danpost danpost

2016/7/8

#
As an alternative to the above, there is a "trick" you can use instead of removing and re-spawning an object -- just relocate the original object to the location you would add the new one at. That way the reference field will still refer to the one in the world.
Octavius Octavius

2016/7/9

#
thanks! it works now.
You need to login to post a reply.