GMS 2 Animation help?

R

Rhqusa

Guest
I'm trying to make a game but I cannot get the animations to work. I have the step event right here:
Code:
/// @description movement test

var hinput_ = keyboard_check(vk_right) - keyboard_check(vk_left);
var vinput_ = keyboard_check(vk_down) - keyboard_check(vk_up);
if vmove_ > 0 {
    hspeed_ = 0
} else if hinput_ != 0 {
    hspeed_ = hinput_ * 3
    hmove_ = 1
} else {
    hspeed_ = 0
    hmove_ = 0
}
if hmove_ > 0 {
    vspeed_ = 0
} else if vinput_ != 0 {
    vspeed_ = vinput_ * 3
    vmove_ = 1
} else {
    vspeed_ = 0
    vmove_ = 0
}

direction_ = round(direction/90)


if place_meeting(x+hspeed_, y, o_solid){
    while !place_meeting(x+sign(hspeed_), y, o_solid){
     x+= sign(hspeed_)
    }
    hspeed_ = 0;
}
if place_meeting(x, y+vspeed_, o_solid){
    while !place_meeting(x, y+sign(vspeed_), o_solid){
     y+= sign(vspeed_)
    }
    vspeed_ = 0;
}

x += hspeed_;
y += vspeed_;

ani_start = direction_*ani;
ani_end = ani_start+2;
if image_index>ani_end {
    image_index = ani_start;
}
if hspeed_>0 or vspeed_>0{
    image_speed = 4;
}

if hspeed_=0 or vspeed_=0{
    image_speed = 0;
    image_index = 16 + direction_;
}
And here is my create event:
Code:
/// @description Variables\
keyboard_set_map(ord("W"),vk_up);
keyboard_set_map(ord("A"),vk_left);
keyboard_set_map(ord("S"),vk_down);
keyboard_set_map(ord("D"),vk_right);
hspeed_ = 0;
vspeed_ = 0;
Gravity_ = 2
Accel_ = 2;
maxhspeed_ = 5;
maxvspeed_ = 5;
hmove_ = 0;
vmove_ = 0;
Z = 2
B = 1
direction_ = 0
ani_start = 0;
ani_end = image_number - 1;
ani = 4
I really need help, as I am new to GMS 2 and do not know how to handle animations yet.

The sprite itself has 20 frames (4 walking frames for each cardinal direction [the first 16 frames] and 1 idle frame for each cardinal direction [the last 4 frames])
 

YoSniper

Member
Honestly, I recommend splitting up your sprite into 5 or 8 different sprites. You can change the sprite of your object in runtime by putting in:
Code:
sprite_index = now_this_sprite;
I would have a four-frame sprite for each of the cardinal directions, and either one more sprite for all of the idle images (you would need to set image_speed = 0 for this case) or four more single-frame sprites for the idle sprites.
 
R

Rhqusa

Guest
So I tried to do that but it didn't work

step event:
Code:
/// @description movement test

var hinput_ = keyboard_check(vk_right) - keyboard_check(vk_left);
var vinput_ = keyboard_check(vk_down) - keyboard_check(vk_up);
if vmove_ > 0 {
    hspeed_ = 0
} else if hinput_ != 0 {
    hspeed_ = hinput_ * 3
    hmove_ = 1
} else {
    hspeed_ = 0
    hmove_ = 0
}
if hmove_ > 0 {
    vspeed_ = 0
} else if vinput_ != 0 {
    vspeed_ = vinput_ * 3
    vmove_ = 1
} else {
    vspeed_ = 0
    vmove_ = 0
}




if place_meeting(x+hspeed_, y, o_solid){
    while !place_meeting(x+sign(hspeed_), y, o_solid){
     x+= sign(hspeed_)
    }
    hspeed_ = 0;
}
if place_meeting(x, y+vspeed_, o_solid){
    while !place_meeting(x, y+sign(vspeed_), o_solid){
     y+= sign(vspeed_)
    }
    vspeed_ = 0;
}

x += hspeed_;
y += vspeed_;

if direction = 0 {
    if hmove_>0 {
    sprite_index = walk3
    } else {
    sprite_index = idle3
    }
}
if direction = 90 {
    if hmove_>0 {
    sprite_index = walk2
    } else {
    sprite_index = idle2
    }
}
if direction = 180 {
    if hmove_>0 {
    sprite_index = walk4
    } else {
    sprite_index = idle4
    }
}
if direction = 270 {
    if hmove_>0 {
    sprite_index = walk1
    } else {
    sprite_index = idle1
    }
}
create event:
Code:
/// @description Variables\
keyboard_set_map(ord("W"),vk_up);
keyboard_set_map(ord("A"),vk_left);
keyboard_set_map(ord("S"),vk_down);
keyboard_set_map(ord("D"),vk_right);
hspeed_ = 0;
vspeed_ = 0;
Gravity_ = 2
Accel_ = 2;
maxhspeed_ = 5;
maxvspeed_ = 5;
hmove_ = 0;
vmove_ = 0;
Z = 2
B = 1
direction_ = round((direction/90)+1)
This did not work. Did I do it wrong and can I fix it?
 
Looking at your original code, shouldnt ani_end be ani_start + 3 (not + 2) ( because you have 4 frames of animation).

Also, you are setting the image_speed to 4, which depending on your sprite settings, is probably way to fast.

What I think is happening is, because you image_speed is 4, your image_index is shooting right past ani_end.

So your code:
Code:
if image_index>ani_end {
image_index = ani_start;
}
is always executing and setting the image_index back to ani_start.

Try using your original code, but with a lower image_speed, maximum of 1, or even less just to check its working, like 0.5

e.g.:
Code:
if hspeed_>0 or vspeed_>0{
image_speed = 0.5;
}
 
R

Rhqusa

Guest
Looking at your original code, shouldn't ani_end be ani_start + 3 (, not + 2) ( because you have 4 frames of animation).

Also, you are setting the image_speed to 4, which depending on your sprite settings, is probably way too fast.

What I think is happening is, because you image_speed is 4, your image_index is shooting right past ani_end.

So your code:
Code:
if image_index>ani_end {
image_index = ani_start;
}
is always executing and setting the image_index back to ani_start.

Try using your original code, but with a lower image_speed, maximum of 1, or even less just to check its working, like 0.5

e.g.:
Code:
if hspeed_>0 or vspeed_>0{
image_speed = 0.5;
}
I would do that but I've already separated the sprite and it sorta works. The only thing missing is the directional animations. it plays one animation when it moves left or right, but not up or down
 
Last edited by a moderator:
The only thing missing is the directional animations. it plays one animation when it moves left or right, but not up or down
That's probably because you've got this if statement:
Code:
if hmove_>0 {
but you're using "hmove" for all four directions. Shouldnt two of them (90 and 270) be checking vmove?
 
E

eidobunny

Guest
I would start a new project and ignore all of your complex movement code and just experiment by having pressing right change the animation to something, and pressing left change the animation to something else, etc. Start simple, and build from there. Then you can incorporate your working sprite animation code into your complex movement code. The more complicated your code becomes, the harder it is to trace through and find the problems.
 
R

Rhqusa

Guest
That's probably because you've got this if statement:
Code:
if hmove_>0 {
but you're using "hmove" for all four directions. Shouldnt two of them (90 and 270) be checking vmove?
I found that error and fixed it but still nothing...
 
R

Rhqusa

Guest
I would start a new project and ignore all of your complex movement code and just experiment by having pressing right change the animation to something, and pressing left change the animation to something else, etc. Start simple, and build from there. Then you can incorporate your working sprite animation code into your complex movement code. The more complicated your code becomes, the harder it is to trace through and find the problems.
I don’t wanna do that because if I made a code for animation and then that code didn’t work with my movement code I’d have to start over and I would have wasted my time.
 
At this point you either need to take a step back and re-think your logic and simplify things as @eidobunny said.

Or, I suggest you use the debug mode, set a break point in your animation code, and step through your code line by line and find out where the logic/values are not what you expect them to be, and work out what is happening so you can fix it.

It's a little tricky to work out what's happening with all your different variables interacting with each other from here, that's where the debugger is great for pinpointing what's not working.

I don’t wanna do that because if I made a code for animation and then that code didn’t work with my movement code I’d have to start over and I would have wasted my time.
Man, then I have news for you. There are some parts of my game I have thrown out and re-written from scratch at least half a dozen times! Each time starting fresh was better than trying to debug messy code, because each time I had learned a little bit more than before, and subsequently improved the code bit by bit.

Heck I just re-wrote some animation code myself, and I was glad I started from scratch because the new method is way better than the previous one.

I know it's terrible to have to re-write stuff when you have invested a ton of time and effort into it. But sometimes you just gotta do it.

I'm not saying you have to change what you've got right now...just that its pretty normal to sometimes have to re-write systems when you find they are not working they way you want them to.
 
E

eidobunny

Guest
I don’t wanna do that because if I made a code for animation and then that code didn’t work with my movement code I’d have to start over and I would have wasted my time.
You're doing it to learn...

How can you learn properly if your understanding of a concept is being filtered through buggy code?
 
D

David11

Guest
Like what the guys said above, one way I figure out what ive done wrong is to take it step by step. Let me explain. Now lets say Im working your project, and want to take their advice, but dont want to delete everything. Use /* at the very begining if possible. Turns the whole code into a comment. Then put each peice of code, one at a time in the order you choose, and then run it. For example, again, go simple like eidobunny suggested. Then add each line, checking to see which line is buggy. This porcess is slow and long, but i you dont want to start over, then start simple. <---damn, I hope you understood what I meant :D
 
Top