Log In  

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

P#11163 2015-06-07 03:58 ( Edited 2015-06-08 22:40)

1

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
P#11172 2015-06-07 18:56 ( Edited 2015-06-07 22:56)

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.

P#11179 2015-06-08 11:55 ( Edited 2015-06-08 15:55)
2

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.

P#11181 2015-06-08 13:11 ( Edited 2015-06-08 17:11)

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()

P#11191 2015-06-08 18:40 ( Edited 2015-06-08 22:40)

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

P#125453 2023-02-07 02:42

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

P#125455 2023-02-07 04:16

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 14:53:54 | 0.017s | Q:20