SOLVED Player Attacking Animations not Working Properly Depending on Current Player State

Anorak21

Member
Hi everyone, I recently started making a two-player sword fighting game, and as I was coding the attack animations I came across a couple of problems. If I started the game and immediately pressed the attack button, the animation worked perfectly and didn't loop. However, if I ran around the stage and pressed the attack button while running, the animation would loop forever. If I ran around for a bit, stopped until the player's speed reaches 0, and then pressed the attack button, the animation would play out, but very slowly. It would move at about a frame per 2 seconds. I do not know how to fix this and any help would be appreciated. Most of my code for attacking is from Shaun Spalding's melee attacking tutorial on youtube. I use a state machine for my character, and here is the code for the attacking state:
GML:
xSpeed = 0
ySpeed = 0

//Start of the attack
if (sprite_index != sPlayerOneAttack)
{
    sprite_index = sPlayerOneAttack;
    image_index = 0;
    ds_list_clear(hitByAttack);
}

//Use attack hitbox & check for hits
mask_index = sPlayerOneAttackHB;
var hitByAttackNow = ds_list_create();
var hits = instance_place_list(x, y, oPlayerTwo, hitByAttackNow, false);

if (hits > 0)
{
    for (var i = 0; i < hits; i++)
    {
        //If this instance has not yet been hit by this attack
        var hitID = hitByAttackNow[| i];
        if (ds_list_find_index(hitByAttack, hitID) == -1)
        {
            ds_list_add(hitByAttack, hitID);
            with (hitID)
            {
                instance_destroy(oPlayerTwo);
            }
        }
    }
}
ds_list_destroy(hitByAttackNow);
mask_index = sPlayerOneIdle;

if (scr_AnimationEnd())
{
    playerOneVertState = verticalstate.onGround;
    sprite_index = sPlayerOneIdle;
}
The scr_AnimationEnd mentioned at the bottom is a bit of script from the gamemaker discord that helps end the animation correctly. Here it is:
Code:
var _sprite=sprite_index;
var _image=image_index;
if(argument_count > 0)   _sprite=argument[0];
if(argument_count > 1)  _image=argument[1];
var _type=sprite_get_speed_type(sprite_index);
var _spd=sprite_get_speed(sprite_index)*image_speed;
if(_type == spritespeed_framespersecond)
    _spd = _spd/room_speed;
if(argument_count > 2) _spd=argument[2];
return _image+_spd >= sprite_get_number(_sprite);
Now I don't know if there's something wrong with the animation end script or my code, but I really don't know what to do. I've tried many things, and none of them seem to work. Any help would be appreciated because I'm getting pretty desperate.
Thanks in advance!
 

Anorak21

Member
One more thing, this is how the attack state is entered from my onGround state, which is the state the player is in when touching the ground.
GML:
// Going into attack state

if (attackKeyPressed)
{
    playerOneVertState = verticalstate.attacking;
    xSpeed = 0;
    ySpeed = 0;
}
Also, here is the script in which I handle the player's animations. Maybe there's something wrong here.
Code:
if  (playerOneVertState = verticalstate.onGround)
{
        if (xSpeed == 0)
        {
            //This is our idle state
            sprite_index = sPlayerOneIdle;
        }
    
}
        
        //This is our horizontal state while moving on ground (no xSpeed)
if  (playerOneVertState = verticalstate.onGround)
{
        if (xSpeed != 0)
        {
            sprite_index = sPlayerOneRunning;
            image_speed  = (xSpeed / maxRunSpeed);
        }


}
 

Nidoking

Member
Well, I do see that in the moving state, you set the image_speed, but you don't seem to set it back to 1 for the attacking sprite. That's what's causing the slow animation. That also likely messes with the end animation script, because it's expecting image_speed to be a certain value that it's not. The animation might be looping internally before the custom script ever catches the end of it.
 

Anorak21

Member
Well, I do see that in the moving state, you set the image_speed, but you don't seem to set it back to 1 for the attacking sprite. That's what's causing the slow animation. That also likely messes with the end animation script, because it's expecting image_speed to be a certain value that it's not. The animation might be looping internally before the custom script ever catches the end of it.
Wow that worked! Thank you so much for your help! šŸ˜ƒ
 
Top