GML Help with changing sprite direction left and right

A

arekkusu

Guest
Hello, I'm new to programming and really need help.
I'm making a platformer and I'm having a problem inserting an additional run animation for the left direction. I made the sprite run to the right, even jump rightwards, but can't seem to do the same for the other direction without canceling the previous script.

Code:
//Get Player Input
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_space);

//Calculate Movement
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,o_wall)) && (key_jump)
{
    vsp = -7;
}

//Horizontal Collision
if (place_meeting(x+hsp,y,o_wall))
{
    while (!place_meeting(x+sign(hsp),y,o_wall))
    {
        x = x + sign(hsp);
    }   
    hsp = 0;
}
x = x + hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,o_wall))
{
    while (!place_meeting(x,y+sign(vsp),o_wall))
    {
        y = y + sign(vsp);
    }   
    vsp = 0;
}
y = y + vsp;

//Animation
if (!place_meeting(x,y+1,o_wall))
{
    sprite_index = sPlayerA;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
    image_speed = 1;
    if (hsp == 0)
    {
        sprite_index = s_player;
    }
    else
    {
        sprite_index = s_playerR;
    }
}
All that is on the player object.
 

Alexx

Member
Try something like, depending on value of walksp (I'm assuming it is 1):
Code:
if (hsp == 0)
    {
        sprite_index = s_player;
    }
else if hsp==1
    {
        sprite_index = s_playerR;
    }
else
   {
        sprite_index = s_playerL;
    }
Also don't bump your own topics, give it a few days and only bump if you have new info.
It is in violation of the guidelines, and will generally get you ignored.
 
Last edited:
A

arekkusu

Guest
Try something like, depending on value of walksp (I'm assuming it is 1):
Code:
if (hsp == 0)
    {
        sprite_index = s_player;
    }
else if hsp==1
    {
        sprite_index = s_playerR;
    }
else
   {
        sprite_index = s_playerL;
    }
Also don't bump your own topics, give it a few days and only bump if you have new info.
It is in violation of the guidelines, and will generally get you ignored.
Walksp is actually 4. Does that change anything?
 
A

arekkusu

Guest
Thank you for replying. I tried the code but it broke the game. I guess my naming might've confused you

s_player - still sprite right
s_playerR - running animation to the right
s_playerA - jump action to the right
s_player_L - still sprite left
s_playerR_L - running animation to the left
s_playerA_L - jump action to the left
 

Alexx

Member
The logic you need is:

Well, first up for setting s_player and s_player_L you will have to remember the last direction the player pressed, and set if the player isn't moving.

For movement sprite check if hsp is equal to 4 or -4 and set the appropriate walking sprite.
If the hsp is more than 4 or less than 4, set the appropriate running sprite.

Have a go yourself, post what you come up with.
 
A

arekkusu

Guest
The logic you need is:

Well, first up for setting s_player and s_player_L you will have to remember the last direction the player pressed, and set if the player isn't moving.

For movement sprite check if hsp is equal to 4 or -4 and set the appropriate walking sprite.
If the hsp is more than 4 or less than 4, set the appropriate running sprite.

Have a go yourself, post what you come up with.
Thank you, but could you give me a line of codes to use? I don't know how to make those checks. As for lines I though of myself:
Code:
if (hsp != 0) && key_left = 0
{
    sprite_index = s_player;
}   
else
{
    sprite_index = s_playerR_L;   
}
Code:
if( hsp < 0 && sprite_index != s_playerR_L ) sprite_index = s_playerR_L;
if( hsp > 0 && sprite_index != s_playerR ) sprite_index = s_playerR;
if keyboard_check(vk_left)
{
sprite_index = s_playerR_L;
}

Neither did the job.
 

Alexx

Member
I'll do an example for you.
Give me 10 minutes or so.
I'll update this post when done.

edit:
Here is a basic system. I've tried to leave in as much of your code as possible.
I removed jumping code, as not relevant here.

I renamed your sprites, to make it easier to read.

I did a Create Event with the following:
Code:
lastdir=1;
walksp=1;
vsp=1;
grv=1;
hsp=0;
And the Step Event is now:
Code:
/// @description Insert description here
// You can write your code in this editor
//Get Player Input
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_space);
//remember last direction
if key_left lastdir=-1;
if key_right lastdir=1;
//Calculate Movement
var move = key_right - key_left;
hsp = hsp +(move * walksp);



///sprite control
//if not moving
if move==0
{
    if lastdir<0
    {
        sprite_index=spr_player_idle_left;  
    }
    else
    {
        sprite_index=spr_player_idle_right;  
    }
}

//if running
else if abs(hsp)>10
{
    if lastdir=-1
    {
        sprite_index=spr_player_run_left;  
    }
    else
    {
        sprite_index=spr_player_run_right;  
    }
}

//walkinh
else
{
    if lastdir=-1
    {
        sprite_index=spr_player_walk_left;  
    }
    else
    {
        sprite_index=spr_player_walk_right;  
    }
}

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while (!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x = x + sign(hsp);
    }  
    hsp = 0;
    //prevent animation next to a wall - you may or may not need this
    if lastdir=-1
    {
        sprite_index=spr_player_idle_left;  
    }
    else
    {
        sprite_index=spr_player_idle_right;  
    }
}


//move
if move!=0
{
    x=x+hsp;
}
else
{
    hsp=0;  
}
It's not perfect, but it does what you asked.
Edit: Made a change to prevent animation when next to a wall.
 
Last edited:
Top