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

2017/11/27

Respawn enemies

tstockun tstockun

2017/11/27

#
Hello! I am making a game in which a ship fires a bullet at another ship which destroys it. I need the ship to respawn on the right side of the screen once it is destroyed. At this point, when the ship is destroyed, it causes an error. Please help as soon as possible!
import greenfoot.*;

public class Red extends Mover
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    
    private int speed = -10; // random speed
    private int direction = 5; // random direction 
   
    private int time = 0;
    
    private int getRandomNumber;
    private int xMinimum;
    private int yMinimum;
    
    public Red()
    {
        image1 = new GreenfootImage("Redship1.png");
        image2 = new GreenfootImage("Redship2.png");
        image3 = new GreenfootImage("Redship3.png");
        image4 = new GreenfootImage("Redship4.png");
        image5 = new GreenfootImage("Redship5.png");
        image6 = new GreenfootImage("Redship6.png");
        setImage (image1);
    }
    
    public void act() 
    {
        animate();
        setLocation((getX()+getWorld().getWidth()+speed+direction)%getWorld().getWidth(), getY());
        checkCollision();
        ifatWorldEdge();
    }
    
    public void animate()
    {
        time++;
        int delay = 5;
        if(time%delay==0)
        {
            if ( getImage()  ==  image1 )  {
                setImage  (image2);
            }
            else if ( getImage()  ==  image2 )  {
                setImage  (image3);
            }
            if ( getImage()  ==  image3 )  {
                setImage  (image4);
            }
            else if ( getImage()  ==  image4 )  {
                setImage  (image5);
            }
            if ( getImage()  ==  image5 )  {
                setImage  (image6);
            }
            else if ( getImage()  ==  image6 )  {
                setImage  (image1);
            }
        }
    }
    
    private void checkCollision()
    {
        Actor b = (Bullet) getOneIntersectingObject (Bullet.class);
        
        if (b !=null)
        {
            World space = getWorld();
            getWorld().removeObject(this);
            Space.scoreCounter.add(50);
            Greenfoot.playSound("Explosion.wav");
        }
    }
    
    private boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    
    public void ifatWorldEdge()
    {
    }
}
Super_Hippo Super_Hippo

2017/11/27

#
You can either add a new object when you remove this one (line 74), or instead of removing, you move this object back to the right edge.
tstockun tstockun

2017/11/27

#
It runs now. Thanks!
You need to login to post a reply.