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

2016/5/12

Making my turtle move with "atEdgeOfWorld"

Orko Orko

2016/5/12

#
Hello I am new to Greenfoot programming and I want help for my lizard to move at edge of the world. My teacher told me to look up the code given in the "Creature" Class - "public class Creature extends Actor { public boolean atEdgeOfWorld() { return getX()<5 || getY()<5 || getX()>getWorld().getWidth()-5 || getY()>getWorld().getHeight()-5; } }" which I did but couldn't understand anything. It's from the game "TurtleAndLizard" ; I want it to turn 7 degrees. please response quickly as I have to submit it tomorrow first thing in the morning. Thank You very much : )
public class Creature extends Actor
{
    public boolean atEdgeOfWorld()
    {
        return getX()<5 || getY()<5 || getX()>getWorld().getWidth()-5 || getY()>getWorld().getHeight()-5;
    }   
}
danpost danpost

2016/5/12

#
Here, you are showing a method that you can call that returns a true/false value (boolean) that lets you know if the actor is at the edge of the world or not. Using its returned value as the condition to turn:
if (atEdgeOfWorld()) turn(7);
You need to login to post a reply.