1

I have a small script that changes the sprite index when I press a certain key.

 if (key_right)
    {
    sprite_index = playerRightSpr;//<-------------
    image_speed = 1;                           //|
    }                                          //|
    if (key_left)                              //|
    {                                          //|
    sprite_index = playerLeftSpr;              //|
    image_speed = 1;                           //|
    }                                          //|
    if (key_left) && (key_right)               //|
    {                                          //|
    sprite_index = playerSpr;                  //|
    image_speed = 0;                           //|
    }                                          //|
    if (!key_right or key_left) //add this and this, doesn't work
    {                           //but it does when I remove this code.
    sprite_index = playerSpr;
    image_speed = 0;
    }

Is there another way to say when standing still make sprite playerSpr because the way i'm trying seems to cause conflict. Thanks in advance Bhodi

  • 1
    Looks like your bottom if statement is missing the `!` on the last statement, that looks likely the cause. As `!` should be declared for each statement. I've also a different solution I can show in case it didn't worked out. – Steven Sep 19 '18 at 05:59
  • That didn't work, could you please tell me the other solution? – AnUnnamedPerson Sep 19 '18 at 06:50

2 Answers2

2

If your entity moves you can just use the speed variable to make this. For example :

  • if your entity has a positive speed (go right) you will use PlayerRightSpr

  • if your entity has a negative speed (go left) you will use PlayerLeftSpr

  • And if speed is 0 you use PlayerSpr

(Instead of using two different sprites you can use image_xscale too)

image_xscale = 1; (normal sprite)

image_xscale = -1; (flip on x axis : mirror)

And finally instead of this :

if (!key_right or key_left)

Use this :

if (!key_right || !key_left)

Or this :

if (key_right == 0 || key_left == 0)

But you are right it's not the correct way to do this

I hope this way is good

1

Here's my second solution. This went from my own project, so it does require changing the whole code.

I've explained each section with comments next to each lines.

Step event:

var hinput = 0; //hinput = Horizontal Input
hinput = keyboard_check(vk_right) - keyboard_check(vk_left); //the trick I've used to declare the movement system, based on the arrow keys. 
                                                             //pressing right = 1, pressing left = -1, pressing both or none = 0.

if (hinput != 0) //it is not 0, so the player is moving
{
    sprite_index = s_player_walk; //changes the current sprite to the walking animation
    image_xscale = hinput;        //changes the direction it's facing: 1 = right, -1 = left.
                                  //when it's 0, it keeps the last direction it faced.
} 
else //the player is not moving
{
    sprite_index = s_player_idle; //changes the current sprite to the idle animation
}

As for the images itself. I've used 2 seperate sprites for this:
s_player_idle, which exists of only 1 frame, and
s_player_walk, which exists of 3 looping frames.

The image speed is already defined in the image editor for each seperate sprite, so it doesn't need to be defined again in the code.

Steven
  • 1,996
  • 3
  • 22
  • 33