Is there an easy way to flip sprites both horizontally AND/OR vertically via code?


I used this function in the river riddle to h flip sprites.
It only works for 8x8 but you can easily translate for other size or for flip vertical
function fspr(fr,ex,ey) for x=0,7 do sx=8*(fr%16) sy=8*(flr(fr/16)) sspr(sx+7-x,sy,1,8,ex+x,ey) end end |


For horizontal flip, sspr() has a flip parameter flag but it does not seem to work. Maybe this will be fixed in v0.1.1.
Benjamin, a minor suggestion for your example: I think you should define sx and sy as local variables in order to avoid creating more costly global variables.


After some research in the forum, I have found this thread describing a direct way to flip a sprite horizontally: https://www.lexaloffle.com/bbs/?tid=1859
In short, there is an undocumented flip parameter to spr:
spr n x y [w h [flip]] |
When flip is set to TRUE, it flips the displayed sprite.
-- draw a 8x8 flipped sprite at pixel location (32,32) spr(1,32,32,1,1,true) -- draw a 16x16 flipped sprite at pixel location (32,32) spr(1,32,32,2,2,true) |
Strangely, this extra parameter is documented for sspr but has no effect on it. So maybe it is just a documentation issue. Maybe Zep can comment about it.
Anyway, thanks to QuantumFlux42 for this trick.


The sspr() behaviour in 0.1.0 is a bug in the implementation rather than a mistake in the docs.
0.1.1 has working fiip_h and flip_v parameters (for horizontal and vertical flipping), and also allows you to use negative target dimensions with sspr()


How would i get a player character in a game to flip horizontally when the l or r arrow key pressed?


pico-8 can use the keyboard, but in general it’s best to use only gamepad to be compatible with all computers and devices that play pico-8. we pretend that the normal mode is gamepad, and developer’s mode adds keyboard and mouse.
in _update, check keys (arrows and buttons, or keyboard keys if you want) and save a variable to remember if you should flip (same way you would save player x/y when moving)
in _draw, call spr with all params up to flip_h: https://www.lexaloffle.com/dl/docs/pico-8_manual.html#SPR
[Please log in to post a comment]