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

2015/4/8

How to have an actor bound in an unbounded world?

cpo123 cpo123

2015/4/8

#
So I have a game in which has an unbounded world so that objects can go off screen. However, I do not want the player to have the ability to go off screen. How can I make just one actor stuck inside the edges of the world? Any help greatly appreciated, thank you!
danpost danpost

2015/4/8

#
At the end of the 'act' method for the actor you want bound, check its coordinate values and if outside limits, set to limit. There are four limits (left, right, up and down). For example (left):
if (getX() < 0) setLocation(0, getY());
cpo123 cpo123

2015/4/8

#
Thanks so much, it works perfectly!
You need to login to post a reply.