• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Possible to animate flipping a sprite"

T

tnuki

Guest
Sorry if this is dumb, I just started using GM2 a couple weeks ago. Basically I know about "flipping" a sprite on the x-axis with the image_xscale command, but that's a single frame 'snap' from 1 to -1 (or vice versa). Is there a way to smooth the transition, i.e. more smoothy transition from 1 to -1 over multiple frames? Kind of like how it works in the Paper Mario games (but in 2D).
 
It's quite simple, you just use the numbers between 1 and -1. I used this myself when doing a similar effect.

Code:
//Larger decimal values make the flip faster
image_xscale -= .05;
 
T

tnuki

Guest
It's quite simple, you just use the numbers between 1 and -1. I used this myself when doing a similar effect.

Code:
//Larger decimal values make the flip faster
image_xscale -= .05;
Alright thanks, that's good to know, I forgot to mention I'm using the sign command to say either 1 or -1 (following Shaun Spalding's tutorials), so what I have currently is
Code:
if (hsp != 0)  image_xscale = sign(hsp);
Where hsp is horizontal movement.
 
Alright thanks, that's good to know, I forgot to mention I'm using the sign command to say either 1 or -1 (following Shaun Spalding's tutorials), so what I have currently is
Code:
if (hsp != 0)  image_xscale = sign(hsp);
Where hsp is horizontal movement.
In that case, you're going to need to decouple sprite flipping from movement. Perhaps something like this:
Code:
// This code assumes your sprites are facing right
if (hsp != 0)
  flip_xscale = sign(hsp);
else
  flip_xscale = 0;

// Flip code
// This is very simple; you might want to modify it to make it look better
image_xscale = clamp(image_xscale + (flip_xscale * flip_speed), -1, 1);
 
T

tnuki

Guest
In that case, you're going to need to decouple sprite flipping from movement. Perhaps something like this:
Code:
// This code assumes your sprites are facing right
if (hsp != 0)
  flip_xscale = sign(hsp);
else
  flip_xscale = 0;

// Flip code
// This is very simple; you might want to modify it to make it look better
image_xscale = clamp(image_xscale + (flip_xscale * flip_speed), -1, 1);
Nice! I've been thinking about this for hours but couldn't figure it out myself, thanks! Maybe I'll try to reverse engineer it so I better understand what exactly it means (rather than just pasting it in)
 
Nice! I've been thinking about this for hours but couldn't figure it out myself, thanks! Maybe I'll try to reverse engineer it so I better understand what exactly it means (rather than just pasting it in)
Glad you're putting in the effort to learn! Sorry I didn't comment properly. I just kind of threw it together, haha. Here it is with better comments:
Code:
// This code assumes your sprites are facing right
if (hsp != 0) // Check for movement
  flip_xscale = sign(hsp); // Sets a variable to begin flipping left (-1) when hsp is smaller than 0, begins flipping right (1) when hsp is larger than 0
else // No movement
  flip_xscale = 0; // If this variable is set to 0, the sprite will not flip

// Flip code
// clamp(val, min, max) keeps a value between a minimum and maximum.
// Since we want to face left and right, this means the boundaries are -1 (full left) and 1 (full right).
// Anything larger makes the sprite look fat and distorted.
image_xscale = clamp(image_xscale + // Adding current image_xscale to the equation makes this code relative
                     (flip_xscale * flip_speed), // Flips to the right if flip_xscale == 1 and to the left if flip_xscale == -1. Does not change if flip_xscale == 0 since 0*n = 0.
                     -1, // Left flip boundary
                     1); // Right flip boundary
 
T

tnuki

Guest
Glad you're putting in the effort to learn! Sorry I didn't comment properly. I just kind of threw it together, haha. Here it is with better comments:
Code:
// This code assumes your sprites are facing right
if (hsp != 0) // Check for movement
  flip_xscale = sign(hsp); // Sets a variable to begin flipping left (-1) when hsp is smaller than 0, begins flipping right (1) when hsp is larger than 0
else // No movement
  flip_xscale = 0; // If this variable is set to 0, the sprite will not flip

// Flip code
// clamp(val, min, max) keeps a value between a minimum and maximum.
// Since we want to face left and right, this means the boundaries are -1 (full left) and 1 (full right).
// Anything larger makes the sprite look fat and distorted.
image_xscale = clamp(image_xscale + // Adding current image_xscale to the equation makes this code relative
                     (flip_xscale * flip_speed), // Flips to the right if flip_xscale == 1 and to the left if flip_xscale == -1. Does not change if flip_xscale == 0 since 0*n = 0.
                     -1, // Left flip boundary
                     1); // Right flip boundary
Great! Thanks again.
 
Top