0
\$\begingroup\$

I am developing a game where my player has to look to see forward when I am pressing right arrow key. But how can I make it look reverse after pressing left arrow? I can create a sprite of reverse look but how can load that while pressing left and at the same time forward player should be destroyed?

\$\endgroup\$
1
\$\begingroup\$

Usually I set the sprite's scale.x to -1 to flip it horizontally. You can see an example here: https://phaser.io/examples/v2/sprites/scale-a-sprite some of those sprites are scaled to negative values, and they appear mirrored.

sprite scale

Another approach is to flip the sprites in your assets. This has the added advantage of looking correct if the character is handed - if they were holding a gun, they won't be switching their gun hand depending on which direction they're facing. Phaser's platformer example does this, and has separate left and right animation frames:

phaser dude

\$\endgroup\$
1
  • \$\begingroup\$ I ran into some weird issues with setting X to -1. I just mirrored my image to create "left-walk" animation. thanks. \$\endgroup\$ – BatteryAcid Aug 21 '18 at 21:56
0
\$\begingroup\$

When your character is looking rightward, his width is positive. So, all you need to do is to invert proportionally his width.

Visual example:

This image's width is 779px

And this image's width is -779px

enter image description here

When reversing, also remember to add the X-axis by the width, so that the character's sprite doesn't "walk" behind the total units that the width of the sprite has

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.