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

2018/5/9

Help stopping an Actor

GreenfootHelp GreenfootHelp

2018/5/9

#
Hi, so I have a program that is basically code from the Rocket ship tutorial one. I added SpaceWombats to attack the Rocket but the game will not stop when the Wombat gets to the Rocket.
import greenfoot.*;

/**
 * Write a description of class SpaceWombat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWombat extends Actor
{

    public void act() 
    { 

        if(Greenfoot.getRandomNumber(100) < 10)
            turn(Greenfoot.getRandomNumber(90) - 45 );
        if(getX() <= 10 || getX() >= getWorld().getWidth() - 10){
            turn(180); move(2);}
        move(4);
    }

    public void hitrocket () { 
        Space spaceWorld = (Space) getWorld(); 
    }

    public void touch() { if (isTouching(Rocket.class)) 
        {  
            Greenfoot.stop();
            getWorld().removeObject(this);
            getWorld().removeObject(this);
            Greenfoot.stop(); 
        }
    }

    public void rocket ()
    {
        int ypos = getY();
        if (ypos > 0) {
            ypos = ypos - 5;
            setLocation(getX(), ypos);
            Actor Rocket = getOneIntersectingObject(Rocket.class);
            if (Rocket != null) {
                hitrocket();
                getWorld().removeObject(Rocket);
                getWorld().removeObject(this);
                Greenfoot.stop(); 

            }
            else {
                getWorld().removeObject(this);
                Greenfoot.stop(); 
            }
        }
    }	
}   

danpost danpost

2018/5/9

#
GreenfootHelp wrote...
Hi, so I have a program that is basically code from the Rocket ship tutorial one. I added SpaceWombats to attack the Rocket but the game will not stop when the Wombat gets to the Rocket. << Code Omitted >>
Look at your act method. While doing so, ask yourself when the other methods (touch, hitrocket and rocket) will execute. Keep in mind, the greenfoot framework only calls the act method (in general) to execute.
You need to login to post a reply.