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

2015/9/19

Stopping A Method

ZoSo ZoSo

2015/9/19

#
i was told that adding the return statement in a conditional statement inside a method will stop executing that method. it doesnt seem to work for me. is this true? or am i dong something wrong? heres my code:
import greenfoot.*;

/**
 * Write a description of class Alien here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Alien extends Actor
{
    private int timer = 5;
    private Counter myCounter;

    public Alien(Counter counter)
    {
        myCounter = counter;
    }

    /**
     * Act - do whatever the Alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        setLocation(getX()-5, getY());
        if (getX()<=0)
        {
            setLocation(getWorld().getWidth()+20, Greenfoot.getRandomNumber(400));

        }

        killEnemy();
        hit();

    }    

    public void killEnemy()
    {
        if (getOneIntersectingObject(Missile.class)!=null)
        {
            getWorld().addObject(new Explosion(), getX(), getY());
            Greenfoot.playSound("explosion.wav");
            Actor missile = getOneIntersectingObject(Missile.class);
            getWorld().removeObject(missile);

            resurrect();

        }
    }

    public void hit()
    {
        if (getOneIntersectingObject(PlayerShip.class)!=null)
        {
            timer--;
            if (timer == 0)
            {
                getWorld().addObject(new Explosion(), getX(), getY());
                Greenfoot.playSound("explosion.wav");

                resurrect();
                timer = 5;
            }
        }
    }

    public void resurrect()
    {
        setLocation(getWorld().getWidth()+20, Greenfoot.getRandomNumber(400));

        if (myCounter.getValue()<=0)
        {

            return;
        }
    }
}
danpost danpost

2015/9/20

#
ZoSo wrote...
i was told that adding the return statement in a conditional statement inside a method will stop executing that method. it doesnt seem to work for me. is this true? or am i dong something wrong?
Yes, it is true. As far as doing something wrong, I would say -- yes ... and no! The code itself is okay as far as stopping the method when the condition is true. However, the code (lines 72 through 76) is placed as the last thing in the method. Therefore, you are not preventing any additional code from executing within the method when the condition becomes true. In other words, there is no difference between having those lines in your code and not having them there. The 'return' statement is used to exit out of the current method causing execution to return back to the calling statement immediately without any following code after the return statement executing. As a side-note, I do not see any code within the class that changes the value of the Counter object, 'myCounter'. So, I cannot tell what its value may be or how the condition of its value being less than or equal to zero would change.
ZoSo ZoSo

2015/9/20

#
thanks, and as far as the conter, i have another class that adds -1 to it
ZoSo ZoSo

2015/9/20

#
actually, the method doesnt stop when i put it as the first thing in the method :/ is it because the other methods are calling it? heres my code where the counter is btw:
import greenfoot.*;

/**
 * Write a description of class PlayerShip here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerShip extends Actor
{
    private int canShoot=0;
    private int lives = 3;
    private int timer = 5;
    private Counter myCounter;

    public PlayerShip(Counter counter)
    {
        myCounter = counter;
        myCounter.setValue(lives);
    }

    /**
     * Act - do whatever the PlayerShip wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveUp();
        moveDown();
        moveLeft();
        moveRight();
        fire();
        canShoot--;

        death();
    }    

    private void moveUp()
    {
        if (Greenfoot.isKeyDown("w"))
        {       
            setLocation(getX(),getY()-5);
        }
    }

    private void moveDown()
    {
        if (Greenfoot.isKeyDown("s"))
        {       
            setLocation(getX(),getY()+5);
        }
    }

    private void moveLeft()
    {
        if (Greenfoot.isKeyDown("a"))
        {       
            setLocation(getX()-5,getY());
        }
    }

    private void moveRight()
    {
        if (Greenfoot.isKeyDown("d"))
        {       
            setLocation(getX()+5,getY());
        }
    }

    public void fire()
    {
        if (Greenfoot.isKeyDown("space")&&(canShoot<=0))
        {
            getWorld().addObject(new Missile(), getX(), getY());
            Greenfoot.playSound("laser.wav");
            canShoot = 25;
        }

        if (Greenfoot.isKeyDown("shift"))
        {
            getWorld().addObject(new Missile(), getX(), getY());
            Greenfoot.playSound("laser.wav");

        }
    }

    public void death()
    {

        if (getOneIntersectingObject(Alien.class) !=null)
        {
            timer--;
            if (timer == 0)
            {
                myCounter.add(-1);
                timer = 5;
            }
        }

        
    }
}
ZoSo ZoSo

2015/9/20

#
nevermind, i got it now :)
You need to login to post a reply.