• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GML Having problem with sprite animation

A

Agletsio

Guest
Ok so I recently did Shaun Spalding's "Platformer Basics" tutorial in an attempt to learn how to work with gravity and jumping. Everything concerning the tutorial went perfect but now I'm not sure how to approach my sprite animation.

Below you will find my current code layout in step event of Player (everything up until "///Sprite Control" is part of the tutorial.) The result of this is that both "walk" sprites work but when my Player is in idle it always faces right (idle_right).

At first I thought that something in the tutorial code was causing the engine to think I'm facing always facing right when not moving but now I'm thinking that the "idle" code is simply just producing a result and the sprite reverts back to "idle_right" because it's my default sprite.

My aim is to call the idle_sprite in which direction I last faced (so when moving left and stopping, it would be "idle_left" and vice versa. Having trouble figuring this out so any help or info would seriously be appreciated!


///Player Input & Gravity

//Player Input

key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);


//React to inputs

move = key_left + key_right;
hsp = move * movespeed;

if (vsp < 20) vsp += grav;
if (place_meeting(x,y+1,obj_wall))
{
vsp = key_jump * -jumpspeed
}


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


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


///Sprite Control

if keyboard_check (vk_left)
{
sprite_index = walk_left;
}

else
if keyboard_check (vk_right)
{
sprite_index = walk_right;
}

else

if direction = 0
{
sprite_index = idle_right;
}

else

if direction = 180
{
sprite_index = idle_left;
}
 

Gillen82

Member
Try this

Code:
///Sprite Control

if keyboard_check (vk_left)
{
    sprite_index = walk_left;
}

if keyboard_check (vk_right)
{
    sprite_index = walk_right;
}

if !keyboard_check (vk_left) && !keyboard_check (vk_right) && direction == 0
{
    sprite_index = idle_right;
}

if !keyboard_check (vk_left) && !keyboard_check (vk_right) && direction == 180
{
    sprite_index = idle_left;
}
 
A

Agletsio

Guest
Try this

Code:
///Sprite Control

if keyboard_check (vk_left)
{
    sprite_index = walk_left;
}

if keyboard_check (vk_right)
{
    sprite_index = walk_right;
}

if !keyboard_check (vk_left) && !keyboard_check (vk_right) && direction == 0
{
    sprite_index = idle_right;
}

if !keyboard_check (vk_left) && !keyboard_check (vk_right) && direction == 180
{
    sprite_index = idle_left;
}


Thanks for the reply Gillen82!

Unfortunately, the result is still the same.

I don't know if this code (from Player create event) is influencing it somehow?

///Variables

grav = 1;

hsp = 0;

vsp = 0;

jumpspeed = 13;

movespeed = 6;
 
S

SyntaxError

Guest
Because you are using your own vsp and hsp variables, I think the inbuilt direction variable isn't modified.

If so, you could try...
Code:
///Sprite Control

if keyboard_check (vk_left)
{
   sprite_index = walk_left;
}

if keyboard_check (vk_right)
{
   sprite_index = walk_right;
}

if !keyboard_check (vk_left) && !keyboard_check (vk_right)
{
   if sprite_index == walk_right sprite_index = idle_right;
   else if sprite_index == walk_left sprite_index = idle_left;
}
 
Last edited by a moderator:

Gillen82

Member
Just made a quick system. Here's your solution.

Code:
if keyboard_check (vk_left)
{
   sprite_index = walk_left;
}

if keyboard_check (vk_right)
{
   sprite_index = walk_right;
}

if !keyboard_check (vk_left) && sprite_index == walk_left
{
   sprite_index = idle_leftt;
}

if !keyboard_check (vk_right) && sprite_index == walk_right
{
   sprite_index = idle_right;
}
Give that a try and let me know how you get on!!
 
Try and change if direction = 0 to

if direction = 0 or direction = 360

OR

swap out facing left and facing right, and make the last option simply "else".

OR

you have the player facing 0 degrees in each case. You have him facing right and moving backwards when moving left, and facing right and moving forwards when moving right. It may appear that the direction is 180, but it is 0 with a negative speed.
 
A

Agletsio

Guest
Yo @Gillen82 and @SyntaxError, your solution seems to have worked! Thanks for the help!

Am I right in saying that both of you took a very similar approach? Just want to make sure I actually understand the code and not just copying it :)
You're referencing the sprites instead of direction, right? Because, like SyntaxError says (i think), my vsp and hsp variables are overriding the direction modifiers I set in my original code?

Also, thanks for the input @Nathan Archer! Will give your suggestions a try too! Cool to see how different approaches work

Awesome guys, thanks for helping a newbie out! Sure I'll call upon your assistance again soon!
 

Gillen82

Member
Both @SyntaxError and myself did come from a similar approach, and to be honest, I would have done it the same way as @SyntaxError. I done my slightly different to try and make it more readable and understandable. This is what I love about coding. There's different ways of achieving the same outcome, and there's always a learning to be done. Glad I could have an input with helping you!!
 
Top