Need help with sprite animation!

D

Doooooli

Guest
I've run into some problems with my sprite animation! I'm making a platformer 2D game and i want the character to be able to move to the left and right and change sprite in the way he's looking!

So this is my movement code:

Code:
left = -keyboard_check(ord("A"));
right = keyboard_check(ord("D"));
up = keyboard_check(ord("W"));

var move = left + right;

hsp = move * movespeed;

if (!place_free(x,y+1) && up) {vsp = -movespeed;}
if (vsp < 10) {vsp += grav;}

if (!place_free(x+hsp,y))
{
    while (place_free(x+sign(hsp),y))
    {
    x += sign(hsp);
    }
hsp = 0;
}

if (!place_free(x,y+vsp))
{
    while (place_free(x,y+sign(vsp)))
    {
    y += sign(vsp);
    }
vsp = 0;
}


x += hsp;
y += vsp;
And then a simple

Code:
if(left) then { sprite_index = sprite8 }

if(right) then { sprite_index = sprite7 }
sprite8 is a sprite that changes the players sprite to look to the left! and vice versa for 7! But it doesn't work really! I can't figure out what's wrong! I don't get any error, but the doesn't change any sprite when i press A or D

anyone know what to do? thx!
 

jo-thijs

Member
That's exactly the thing I hate about that code of Shaun Spalding, that he uses this:
Code:
left = -keyboard_check(ord("A"));
...
var move = left + right;
instead of this:
Code:
left = keyboard_check(ord("A"));
...
var move = right - left;
The way you've currently got it, left will either be 0 or -1, which are both interpreted as false in GameMaker.
This makes it that this line never does anything:
Code:
if(left) then { sprite_index = sprite8 }
keeping the sprite at sprite7.
 
D

Doooooli

Guest
That's exactly the thing I hate about that code of Shaun Spalding, that he uses this:
Code:
left = -keyboard_check(ord("A"));
...
var move = left + right;
instead of this:
Code:
left = keyboard_check(ord("A"));
...
var move = right - left;
The way you've currently got it, left will either be 0 or -1, which are both interpreted as false in GameMaker.
This makes it that this line never does anything:
Code:
if(left) then { sprite_index = sprite8 }
keeping the sprite at sprite7.
hmm that does sound wierd, but i tried
Code:
left = keyboard_check(ord("A"));
...
var move = right - left;
still doesn't work :/ the character is still at the same sprite whenever i press A or D :p i might have missed something super obvious, but i'm just here to learn :D also thx for fast response again!
 
D

Doooooli

Guest
I don't see anything else wrong with that code.
Is there any other code that changes sprite_index?
well i do have a
draw_sprite(spr_player,0,real(ds_map_find_value(ds_list_find_value(list,i),"x")),real(ds_map_find_value(ds_list_find_value(list,i),"y")))
} in the draw event, could that be the problem? otherwise i don't have anything that alters the sprite_index or the sprite for that matter :p
 

jo-thijs

Member
Yes, that's indeed the problem.
Change this:
Code:
spr_player,0
to:
Code:
sprite_index,-1
Also, you'll need some code to reset sprite_index to spr_player we both left and right are false.
 
D

Doooooli

Guest
Yes, that's indeed the problem.
Change this:
Code:
spr_player,0
to:
Code:
sprite_index,-1
Also, you'll need some code to reset sprite_index to spr_player we both left and right are false.
THANK YOU! it worked! i just added this:
if(keyboard_check_released(ord("A"))) then { sprite_index = spr_player }
if(keyboard_check_released(ord("D"))) then { sprite_index = spr_player }

so whenever i let go of a button it resets to the spr_player :D
pretty simple
 
Top