Pacman-like Screen Wrapping


(ryme-o) #1

Does anyone how can you make the player go off-screen and make it come back out the opposite direction, for example, if it went off screen while going down, it comes back through the top of the screen, same for Left and Right.


(Ultima2876) #2

A quick example would be (in your player entity’s ‘update’ function, remembering to include net.flashpunk.FP):

if(y > FP.height) { y -= FP.height; }

What that does is test if the y position of the entity has gone beyond the height of the screen. If it has, it moves the player one screen’s height back up. You can tweak this to behave a little better; for instance, you probably actually want to move the player up one screen height + the player object’s height so they don#'t see it suddenly pop in at the top of the screen:

if(y > FP.height) { y -= FP.height + height; }

This way, as long as you have the height set correctly, it will appear from ‘beyond’ the top of the screen in the traditional way.

For moving off the top of the screen and appearing at the bottom, you just do the opposite:

if(y < 0 - height) { y += FP.height; }

And for left/right, you do the exact same stuff but with the x axis:

if(x > FP.width) { x -= FP.width + width; }
if(x < 0 - width) { x += FP.width; }

I haven’t tested that code so you may need to tweak it slightly, but this should get you started :slight_smile:


(ryme-o) #3

Thanks @Ultima2876 I’ll try it out.