Problems with Melee Attack for a Platformer

R

Rosa.Hiru

Guest
I have a problem in which I cannot set sprite_index = spr_player_idle if hsp = 0, because that stops my spr_player_attack animation in the first frame. Thus, I have an issue where if i do the opposite the sprite will walk in place when hsp = 0. Is there a way to have the player sprite idle unless the attack button is pressed?

I also need the hitbox to appear after frame 5 of the attack animation, but if i try "if (image_index >= 5)" then i also have to fix the facing issue of where the hitbox appears. Here's the line of code:

Code:
var xdiff;
var ydiff;

if (mouse_check_button_pressed(mb_left))
{
    swordswing = true;
    
    xdiff = x - xprevious;
    ydiff = y - xprevious;
    
    if (swordswing == true)
    {
        sprite_index = spr_player_slash
        image_index += 0
        
        if (sprite_index = spr_player_slash)
        {
            if image_index = 6
            {
                sprite_index = spr_player_idle;
                image_index += 0;
                
                swordswing = false;
            }
        }
    }
    
    if (!(xdiff == 0) && (ydiff == 0))
    {
        deltax = xdiff;
        deltay = ydiff;
    }
    if (facing >= 0)
    {
            instance_create_layer(x + sign(deltax), y + sign(deltay) + 5, "Instances", obj_hitbox)
    }
    else
    {
        instance_create_layer(x + sign(deltax) - 80, y + sign(deltay) + 5, "Instances", obj_hitbox)
    }
}
 

Yal

🐧 *penguin noises*
GMC Elder
Add a new variable called "busy_in_animation", which is set to true when you start an attack animation (or any other animation you don't want interrupted, like being set on fire from touching fire damage) and set to false when such an animation ends. Wrap all your current animation code in an if statement if not(busy_in_animation){ so it's only executed when you're NOT busy.
 
R

Rosa.Hiru

Guest
Oh, thank you very much!
although, I'm not sure I wrapped it correctly..

Code:
var xdiff;
var ydiff;
var busy_in_anim;

if (sprite_index = spr_player_slash)
{
    busy_in_anim = true
}

if (mouse_check_button_pressed(mb_left))
if not(busy_in_anim)
{
    swordswing = true;
    
    xdiff = x - xprevious;
    ydiff = y - xprevious;
    
    if (swordswing == true)
    {
        sprite_index = spr_player_slash
        image_index += 0
        
        if (sprite_index = spr_player_slash)
        {
            if image_index = 6
            {
                sprite_index = spr_player_idle;
                image_index += 0;
                
                swordswing = false;
            }
        }
    }
    
    if (!(xdiff == 0) && (ydiff == 0))
    {
        deltax = xdiff;
        deltay = ydiff;
    }
    if (facing >= 0)
    {
            instance_create_layer(x + sign(deltax), y + sign(deltay) + 5, "Instances", obj_hitbox)
    }
    else
    {
        instance_create_layer(x + sign(deltax) - 80, y + sign(deltay) + 5, "Instances", obj_hitbox)
    }
}
 
P

ParodyKnaveBob

Guest
Howdy, Rosa.Hiru, and welcome to the GMC. $:^ J

You never set busy_in_anim to false.

I hope this helps!
 

Yal

🐧 *penguin noises*
GMC Elder
Protip: when you have an issue, tell us about how things doesn't work (e.g. in this case "now the player never gets out of the attack animation"), not just that it doesn't work... knowing what the problem is makes it much easier to track down the problem.
 
Top