Legacy GM [SOLVED] Attack only work when I'm holding "attack_key"

P

ProExCurator

Guest
Code:
///scr_move_state
scr_get_input();

//Movement
phy_rotation = -point_direction(x,y,mouse_x,mouse_y)
global.rotation = phy_rotation

var xaxis = (right_key - left_key);
var yaxis = (down_key - up_key);

// Get direction
var dir = point_direction(0, 0, xaxis, yaxis);

// Get the length
if(xaxis == 0 and yaxis = 0) {
    len = 0;
    run_len = 0;
} else {
    len = spd;
    run_len = run_spd;
}

// Get the hspd and vspd
hspd = lengthdir_x(len, dir);
vspd = lengthdir_y(len, dir);
run_hspd = lengthdir_x(run_len, dir);
run_vspd = lengthdir_y(run_len, dir);

if(len == 0) {
    if(!attack_key) {
    sprite_index = spr_player_stand
    }
    global.Direction = "Stand"
    }

// Move
if(keyboard_check(vk_lshift)) {
phy_position_x += run_hspd;
phy_position_y += run_vspd;
} else {
phy_position_x += hspd;
phy_position_y += vspd;
}

// Vertical sprites
if (vspd > 0) {
    if(!attack_key) {
    if(!keyboard_check(vk_lshift)) {
    image_speed = .2;
    sprite_index = spr_player;
    } else {
    image_speed = .3;
    sprite_index = spr_player;
    }
    }
    global.Direction = "Up";
    } else if (vspd < 0) {
    if(!attack_key) {
    if(!keyboard_check(vk_lshift)) {
    image_speed = .2;
    sprite_index = spr_player;
    } else {
    image_speed = .3;
    sprite_index = spr_player;
    }
    global.Direction = "Down";
    }
}
// Horizontal sprites
if (hspd > 0) {
    if(!attack_key) {
    if(!keyboard_check(vk_lshift)) {
    image_speed = .2;
    sprite_index = spr_player;
    } else {
    image_speed = .3;
    sprite_index = spr_player;
    }
    }
    global.Direction = "Left";
    } else if (hspd < 0) {
    if(!attack_key) {
    if(!keyboard_check(vk_lshift)) {
    image_speed = .2;
    sprite_index = spr_player;
    } else {
    image_speed = .3;
    sprite_index = spr_player;
    }
    global.Direction = "Right";
    }
}
if (vspd > 0 && hspd > 0) {
global.Direction = "Up_Left";
}
if (vspd > 0 && hspd < 0) {
global.Direction = "Up_Right";
}
if (vspd < 0 && hspd > 0) {
global.Direction = "Down_Left";
}
if (vspd < 0 && hspd < 0) {
global.Direction = "Down_Right";
}

// Melee Attack
if (global.Select_Weapon = "Melee")
{ 
        if(attack_key) {
        image_index = 0;
        image_speed = 0.5
        sprite_index = spr_player_melee
        if (floor(image_index>=5))
        {
            if (global.MeleeAttacking == false)
            {
                global.MeleeAttacking = true;
                instance_create(x, y, obj_attack_box)
                audio_play_sound(snd_melee_swing, 0, false);
            }
        }
    }
}
Melee attack only work when I'm holding attack_key, why? help please.

Maybe I'm just blind and I don't see something obvious.
 
Last edited by a moderator:

obscene

Member
keyboard_check(keyname) means "hold"

keyboard_check_pressed(keyname) means "pressed"

When you press the attack key, you need to enter an attack state and every step you keep checking that attack state until the animation is complete, regardless of a keyboard check.

/// Pseudocode
if attacking
{
do stuff until done, then set attacking to false
}
else if check keyboard for attack
{
attacking=true
}
 
Top