How do you make the "Targets" in target game from running into the corner

6 views
Skip to first unread message

samps...@gmail.com

unread,
Apr 21, 2015, 3:03:16 PM4/21/15
to greenfoot-dis...@googlegroups.com
Im fairly new to greenfoot and I was wondering, how do you keep the targets from going into the corner of the box and getting stuck

spar...@gmail.com

unread,
Apr 21, 2015, 3:43:49 PM4/21/15
to greenfoot-dis...@googlegroups.com
Use the "atWorldEdge()" method taught in Wombat.

public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
        return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
        return true;
        else
        return false;
    }


And then you use that method for your target:


public void turnback()
    {
         if(atWorldEdge() == true)
            {
           Input your code here.
            }
    }
Reply all
Reply to author
Forward
0 new messages