• 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!

troubles flipping sprite

L

Lofi

Guest
i'm trying to flip my sprite with this script
if keyboard_check(ord("A"))
then
{
image_xscale=-1;
}
else
{
image_xscale=1;
}

so when i hold A my sprite looks the other way but when i let go it stops, i want it to stay looking Left when i stop pressing A
 

obscene

Member
As long as you simply have "else" it will face right in all other situations that are not pressing A. You could add "else if" and check for the D button instead and it should only change when either of those two keys are pressed.
 
GML:
// in create event
flipSprite = 1;

// in step event
if (keyboard_check_pressed(ord("A"))) {
flipSprite = !flipSprite;
}
switch (flipSprite) {
    case 0:
    image_xscale = -1;
    break;

    case 1:
    image_xscale = 1;
    break;
}
you can use a on/off switch and a switch statement to do this, pretty clean. initialize the variable flipSprite to 1 in the create event so it starts facing right, then put the keyboard check in the step event to flip it. doing variable = !variable is a neat little on/off trick, and remember that if you want it to only happen with a single press you gotta check for check_pressed, not check.
 
L

Lofi

Guest
thank you mate i went with this in the end

if keyboard_check(ord("A"))
{
image_xscale=-1;
}
if keyboard_check(ord("D"))
{
image_xscale=1;
}




also why can i not do this
if image_xscale=-1
 
Last edited by a moderator:
Top