• 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!

GML GML attack in air issue

I've run into an issue with my code. I am making a 2D platformer. As of now, when my player character shoots a projectile in the air he freezes in the air. I have tried including my vsp and hsp in the attack state but I still get the same issue. If anyone knows why I'm having this issue I would greatly appreciate the assistance.

Attack script:

GML:
hsp = 0;
vsp = 0;
 


//Attack
    #region attack
if (sprite_index != sPlayerAttack)
{
    sprite_index = sPlayerAttack;
    image_index = 0;
    ds_list_clear(hitByAttack);
    #endregion
    
//Projectile   
    #region projectile

{
var inst = instance_create_layer(x,y, "Player", oTomahawkA);
with (inst)
{
if other.image_xscale == 1
    {
    direction = 0;
    }
else direction = 180;
}
}
    #endregion
        
}




//End attack

if (animation_end())
{
    sprite_index = sPlayer;
    state = playerstate.free;
}

Player free script

GML:
//Calculate Movement
    #region Movement
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting (x,y+1,oWall)) && (key_jump)
{
    vsp = -7;
}
    #endregion
    
//Collisions
    #region Collision
//Horizontal Collision
if (place_meeting (x+hsp,y,oWall))
{
    while (!place_meeting(x+sign(hsp),y,oWall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
}
x = x + hsp;
//Vertical Collision
if (place_meeting (x,y+vsp,oWall))
{
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;
#endregion
    
//Jump
    #region Jump
if (!place_meeting(x,y+1,oWall))
{
    sprite_index = sPlayerA;   
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0;
    
}
else
{
    image_speed = 1;
    if (hsp == 0)
    {
        sprite_index = sPlayer;
    }
    else
    {
        sprite_index = sPlayerR;
    }
}

if (hsp != 0) image_xscale = sign (hsp);
    #endregion
    
//Crouching
    #region Crouch
if (place_meeting (x,y+key_down,oWall))
{
    sprite_index = SplayerD;
    walksp = 0;
}
else
{
    walksp = 4;
}
    #endregion
    
//Play Attack State
    if (key_attack) state = playerstate.attack;

Animation end script
GML:
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[2];
if(argument_count > 2) _spd=argument[2];
return _image+_spd >= sprite_get_number(_sprite)
 
Last edited:

Mk.2

Member
Code:
hsp = 0;
vsp = 0;
Why would you expect anything else than the player's movement freezing when you have this in the attack state? Perhaps you want to stop the player's movement for one frame when they attack, instead of every frame while they're in the attack state? In which case, you should remove that part of the code from the attack state, and put it here:

Code:
//Play Attack State
    if (key_attack) 
    {
         hsp = 0;
         vsp = 0;
         state = playerstate.attack;
    }
 
Code:
hsp = 0;
vsp = 0;
Why would you expect anything else than the player's movement freezing when you have this in the attack state? Perhaps you want to stop the player's movement for one frame when they attack, instead of every frame while they're in the attack state? In which case, you should remove that part of the code from the attack state, and put it here:

Code:
//Play Attack State
    if (key_attack)
    {
         hsp = 0;
         vsp = 0;
         state = playerstate.attack;
    }
Yes, I want the player to pause when they attack. Though the freezing still occurs in the air when I take the code out. Could it have to do with the animation end script perhaps?
 

woods

Member
can you save the hsp and vsp before the player goes into attack state.. and then recall the spd when leaving the attack state.

wouldnt this cause the player to stop..attack..continue?
 
Top