Animation repeating

S

SummerScorcher

Guest
Hi, Im making a game and just got done with making the art and some animations for my main character. Everythings going pretty well, however, there's a couple of bugs I don't know how to indentify the problem/fix.

Im not really sure how to descibe it so I made a video:
(darn, 5-post limit, I guess Ill describe it as best I can: )
I have scripts (states) for my character, one for walk/sprint, jump, fall, and land. When landing, if not holding any of the movement keys (to I guess override the animation) it repeats 2-3 times moving her in the direction she's facing. I can't find why it does this, any help would be very much appreciated.

Code:
//Initiate jump
if keyboard_check(ord("W")) {
    image_index = 0;
    state = scr_LillaJump;
}

//Move
    if keyboard_check(vk_lshift) {
        //Sprint!
        if !(keyboard_check(ord("D")) and keyboard_check(ord("A"))) {
            if keyboard_check(ord("D")) or keyboard_check(ord("A")) {
                if sprite_index != spr_LillaSprint {
                    sprite_index = spr_LillaSprintPre;
                    image_speed = 1;
                    }
                } else {
                if (sprite_index = spr_LillaSprint or sprite_index = spr_LillaSprintPre) {
                    sprite_index = spr_LillaIdle;
                    }
            }
            //Reset speed upon turning left/right
            if (keyboard_check_pressed(ord("A")) or keyboard_check_pressed(ord("D"))) {
                movementspeed = 0;
            }
        
            if keyboard_check(ord("D")) {
                if movementspeed <= 7 {
                 movementspeed += 0.3;
                }
                image_xscale = 1;
                phy_position_x += movementspeed;
            }
        
            if keyboard_check(ord("A")) {
                if movementspeed <= 7 {
                 movementspeed += 0.3;
                }
                image_xscale = -1;
                phy_position_x -= movementspeed;
            }
        } else {
        //Deaccelerate
        if not movementspeed <= 0 {
            sprite_index = spr_LillaIdle;
            if image_xscale = 1 {
                movementspeed -= 0.2;
                phy_position_x += movementspeed;
            } else {
                movementspeed -= 0.2;
                phy_position_x -= movementspeed;
            }
        } else {
                sprite_index = spr_LillaIdle;
                movementspeed = 0;
                image_speed = 1;
            }
        }
    } else {
        //Walk
        if movementspeed > 3 {
            movementspeed -= 0.2;
            image_speed = 2;
            if not image_speed = 1 {
                image_speed -= 0.2;
            }
        } else {
            image_speed = 1;
        }
        if !(keyboard_check(ord("D")) and keyboard_check(ord("A"))) {
            if keyboard_check(ord("D")) or keyboard_check(ord("A")) {
                if sprite_index != spr_LillaWalk {
                    sprite_index = spr_LillaWalkPre;
                    }
                } else {
                if (sprite_index = spr_LillaWalk or sprite_index = spr_LillaWalkPre) {
                    sprite_index = spr_LillaIdle;
                    }
            }
            if keyboard_check(ord("D")) {
                if movementspeed <= 3 {
                 movementspeed += 0.05;
                }
                image_xscale = 1;
                phy_position_x += movementspeed;
            }
        
            if keyboard_check(ord("A")) {
                if movementspeed <= 3 {
                 movementspeed += 0.05;
                }
                image_xscale = -1;
                phy_position_x -= movementspeed;
            }
        } else {
            //Idle
        if not movementspeed <= 0 {
            if image_xscale = 1 {
                movementspeed -= 0.2;
                phy_position_x += movementspeed;
            } else {
                movementspeed -= 0.2;
                phy_position_x -= movementspeed;
            }
        } else {
                sprite_index = spr_LillaIdle;
                movementspeed = 0;
                image_speed = 1;
        }
            }
    }
 
 
    //Prevent animation loop upon releasing both keys
    if keyboard_check_released(vk_lshift) and (keyboard_check_released(ord("D")) or keyboard_check_released(ord("A"))) {
        sprite_index = spr_LillaIdle;
        image_speed = 1;
    }
 
    //Break speed even upon standing still
    if not (keyboard_check(ord("D")) or keyboard_check(ord("A"))) {
        if not movementspeed <= 0 {
            if image_xscale = 1 {
                movementspeed -= 0.2;
                phy_position_x += movementspeed;
            } else {
                movementspeed -= 0.2;
                phy_position_x -= movementspeed;
            }
        } else {
                sprite_index = spr_LillaIdle;
                movementspeed = 0;
                image_speed = 1;
        }
    }

Code:
//Start jumping
if sprite_index != spr_LillaJumpPre {
    sprite_index = spr_LillaJumpPre;
}
if (sprite_index = spr_LillaJumpPre and image_index = 11) {
    phy_speed_y = -10;
}

//Fall
if phy_speed_y > 3 {
    state = scr_LillaFall;
}

//Still able to move
if keyboard_check(ord("D")) {
    if movementspeed <= 5 {
    movementspeed += 0.3;
    }
    image_xscale = 1;
    phy_position_x += movementspeed;
    }
 
    if keyboard_check(ord("A")) {
    if movementspeed <= 5 {
    movementspeed += 0.3;
    }
    image_xscale = -1;
    phy_position_x -= movementspeed;
}

Code:
//Fall
sprite_index = spr_LillaJumpFalling;

//Can still move, but limited
if keyboard_check(ord("D")) {
    phy_position_x += 2;
}

if keyboard_check(ord("A")) {
    phy_position_x -= 2
}

//Land
if place_meeting(phy_position_x, phy_position_y, Wall) {
    state = scr_LillaLandSafe;
}

Code:
//Land safely
if sprite_index != spr_LillaJumpLandSafe {
    sprite_index = spr_LillaJumpLandSafe;
}

//Still able to move
if keyboard_check(ord("D")) {
    phy_position_x += 2;
}

if keyboard_check(ord("A")) {
    phy_position_x -= 2
}

Edit: Nevermind, after a good break and clearing my head I managed to fix all my current bugs! ;)
 
Last edited by a moderator:
T

Ting_Thing

Guest
I didn't comb through all of that code you provided, but I've seen that issue fixed before. Here are two possible problems you may be experiencing.

If your image speed is not divisible by one, then asking "if image_index = 5" or something like that won't do, because the image index may not land on a rounded number. The animation is not "caught" and it continues to loop a couple times before landing on 5.0. Try "if floor(image_index) = 5".

Often times, the land code in Platformer games doesn't properly place the character fully on the ground, removing vertical speed, rounding the y position, etc. This may cause the character to end up just slightly above the ground and re-trigger the landing sequence, hence the animation occurring a couple more times.

Hopefully one of those two issues is what's causing the animation problem. Good luck!
 
Top