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

2013/1/22

Help please Turn at world edge?!?

Snake12163 Snake12163

2013/1/22

#
can someone please help me with this code; if(atWorldEdge()) { turn(3); } why doesn't it work anymore?
askgriff askgriff

2013/1/23

#
Have you added the atWorldEdge method to your new class? It's not a built in function. For example, I have a superclass called "Critters" and I want all critters under that class (i.e. bunnies, snakes, etc.) to access atWorldEdge. I add the following to my superclass:
/**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
she53079 she53079

2013/1/24

#
MIK included the atWorldEdge() method in the Animal class. You probably created a new scenario which does not include such class. Askgriff's suggestion is faster than obtaining the Animal class.
yenni9sutedjo yenni9sutedjo

2013/4/9

#
I have a question on the atWorldEdge script. For example the position of an object is 9 for X and 20 for Y, does it mean that the script will return false value? thanks,
danpost danpost

2013/4/9

#
If using the code askgriff supplied for the 'atWorldEdge' method, then if you have 9 for X, the method will return a 'true' value (meaning it is near the edge of the world).
yenni9sutedjo yenni9sutedjo

2013/4/9

#
Thank you for the reply. It means after any "return" word, all scripts under it will be ignored. Correct me if I am wrong.
danpost danpost

2013/4/9

#
I believe you are thinking correctly: After executing a return, no further statements will be executed in that method and execution resumes on the next line of the calling method.
You need to login to post a reply.