Death slap
Death slap

Reputation: 1

How do I flip my player sprite without having it jump farther than where it needs to be?

I am trying to make an open-world RPG game. I just started it, and I have the player moving and the animations changing between the idle animation and the walking animation. However I just recently coded the flipping of the player on the x-axis, which does work but with one caveat. The player flips just a little bit too far. as if to mimic a long jump. How do I keep the player in the same position after flipping?

I have tried changing the player's scaling. Changing the speed of the player. I have even tried some of the recommended code on the internet which did some wacky things. I can't find anybody else that has this problem.

var move = keyboard_check(vk_right) - keyboard_check(vk_left);
if (move < 0)
{
  sprite_index = spr_char_walking;
  image_xscale = -7;
  image_yscale = 7;// flips the sprite when moving the other way
  
}
else if(move > 0)
{
    sprite_index = spr_char_walking;
    image_xscale = 7;
    image_yscale = 7;
    
}
else
{
  sprite_index = spr_char_idle;
  
  
}

Upvotes: 0

Views: 55

Answers (1)

Chronocidal
Chronocidal

Reputation: 8081

Where is the Origin of your sprite?

By default, newly created sprites have their Origin in the top-left corner. This is the "anchor point" that goes at (x, y) when you draw the sprite, and everything else expands out from there.

When you flip a sprite, you mirror everything, including the position of the Origin. As such, for sprites that you are going to mirror, you usually want the Origin to be in the centre.

Top example: flipping a Sprite with the Origin at (0, 0), the top-left corner.

Demonstrations of Sprite Flipping

Bottom example: flipping the same Sprite, but with the Origin moved to the bottom-centre

Other solutions to this would be to add an Offset when drawing the Sprite if it is flipped (assuming that you are drawing the sprite yourself, rather than using the default Draw Event)

Upvotes: 2

Related Questions