• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Help fixing a movement animation or the code for it?

J

John_Doe

Guest
Hi this past week I've been trying to find a tutorial or information about how to make a Pokémon or final fantasy style grid based movement with animations. I have 90% of that but the animation doesn't play correctly, fully, and it appears to stutter while holding down the directional movement keys. I followed a tutorial from Gamemaker Rob which I have linked below, and I added in the code he put in the comment section to fix it playing the idle every time the character moved to a new tile, as well as something to slow down the animation.


If anyone has experience, code I can use, advice for figuring these kinds of problems out on my own, or really anything for someone new to GMS2 and programming in general, I am happy to hear it.

I've posted a video of the issue below and the code.

 
Hi, @John_Doe

Nice work, I like the style you're going for!

I think I've found the issue. The if statement starting on line 49 looks like it's supposed to encapsulate the entire subsequent single-line if statements, but because there are no braces it is only affecting the one below it, causing unwanted idle sprite setting.

(Just a side, but, I think some people on the forum might have a hard time viewing the code in the video. It might be helpful to post it in text form as well.)

Anyway, thanks for sharing your progress!

[Edit: This forum is a great resource, I've also benefited a lot from HeartBeast's tutorials and some other programming courses on Udemy]
 
Last edited:
J

John_Doe

Guest
Hi, @John_Doe

Nice work, I like the style you're going for!

I think I've found the issue. The if statement starting on line 49 looks like it's supposed to encapsulate the entire subsequent single-line if statements, but because there are no braces it is only affecting the one below it, causing unwanted idle sprite setting.

(Just a side, but, I think some people on the forum might have a hard time viewing the code in the video. It might be helpful to post it in text form as well.)

Anyway, thanks for sharing your progress!

[Edit: This forum is a great resource, I've also benefited a lot from HeartBeast's tutorials and some other programming courses on Udemy]
Hey there, thanks for the response! Your suggestion helped me fix it, I need to play around with the animation speed and things more but this is solved.

GML:
/// @desc Movement code

#region MOVEMENT

if (canMove){
if (keyboard_check(vk_up)){
    targetX = x;
    targetY = y - 32;
    image_speed = 0.5;
    sprite_index = spr_walk_up;
    canMove = false;
}

if (keyboard_check(vk_down)){
    targetX = x;
    targetY = y + 32;
    image_speed = 0.5;
    sprite_index = spr_walk_down;
    canMove = false;
}

if (keyboard_check(vk_left)){
    targetX = x - 32;
    targetY = y;
    image_speed = 0.5;
    sprite_index = spr_walk_left;
    canMove = false;
}

if (keyboard_check(vk_right)){
    targetX = x + 32;
    targetY = y;
    image_speed = 0.5;
    sprite_index = spr_walk_right;
    canMove = false;}
}

if (x != targetX) || (y != targetY){
    if (x < targetX) x = x + stepSpeed; // can also be written shorthand x +=
    if (x > targetX) x = x - stepSpeed;
    if (y < targetY) y = y + stepSpeed;
    if (y > targetY) y = y - stepSpeed;
}else{



    if (x == targetX) && (y == targetY){
        canMove = true;
        if(!keyboard_check(vk_left) && !keyboard_check(vk_right) && !keyboard_check(vk_up) && !keyboard_check(vk_down)){
    if (sprite_index == spr_walk_up) sprite_index = spr_idle_up;
    if (sprite_index == spr_walk_down) sprite_index = spr_idle_down;
    if (sprite_index == spr_walk_left) sprite_index = spr_idle_left;
    if (sprite_index == spr_walk_right) sprite_index = spr_idle_right;
    }

}
}

#endregion
 
Hey there, thanks for the response! Your suggestion helped me fix it, I need to play around with the animation speed and things more but this is solved.

GML:
/// @desc Movement code

#region MOVEMENT

if (canMove){
if (keyboard_check(vk_up)){
    targetX = x;
    targetY = y - 32;
    image_speed = 0.5;
    sprite_index = spr_walk_up;
    canMove = false;
}

if (keyboard_check(vk_down)){
    targetX = x;
    targetY = y + 32;
    image_speed = 0.5;
    sprite_index = spr_walk_down;
    canMove = false;
}

if (keyboard_check(vk_left)){
    targetX = x - 32;
    targetY = y;
    image_speed = 0.5;
    sprite_index = spr_walk_left;
    canMove = false;
}

if (keyboard_check(vk_right)){
    targetX = x + 32;
    targetY = y;
    image_speed = 0.5;
    sprite_index = spr_walk_right;
    canMove = false;}
}

if (x != targetX) || (y != targetY){
    if (x < targetX) x = x + stepSpeed; // can also be written shorthand x +=
    if (x > targetX) x = x - stepSpeed;
    if (y < targetY) y = y + stepSpeed;
    if (y > targetY) y = y - stepSpeed;
}else{



    if (x == targetX) && (y == targetY){
        canMove = true;
        if(!keyboard_check(vk_left) && !keyboard_check(vk_right) && !keyboard_check(vk_up) && !keyboard_check(vk_down)){
    if (sprite_index == spr_walk_up) sprite_index = spr_idle_up;
    if (sprite_index == spr_walk_down) sprite_index = spr_idle_down;
    if (sprite_index == spr_walk_left) sprite_index = spr_idle_left;
    if (sprite_index == spr_walk_right) sprite_index = spr_idle_right;
    }

}
}

#endregion
Awesome, I'm glad to hear that! I wish you the best in your game making endeavors!
 
Top