GameMaker (SOLVED) enemy stops moving

X

xilian7125

Guest
hi there. there are many problems im having with the enemy but this is a big one
when i touch the enemy, both the enemy AND the player get stuck.
code:

PLAYER:

CREATE:
Code:
hsp = 0;
vsp = 0;
grv = 0.4;
walksp = 8;

hp = 100

state = PLAYERSTATE.FREE
hitbyattack = ds_list_create();

enum PLAYERSTATE
{

    FREE,
    ATTACK_SLASH,
    ATTACK_COMBO,
    HIT

}
STEP
Code:
//player input (check for correct symbols)
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_up);
key_attack = keyboard_check_pressed(ord("Z"))

switch (state)

{
    case PLAYERSTATE.FREE: PlayerState_Free(); break;
    case PLAYERSTATE.ATTACK_SLASH: PlayerState_Attack_Slash(); break;
    case PLAYERSTATE.ATTACK_COMBO: PlayerState_Attack_Combo(); break;


}
PLAYERSTATE_FREE
Code:
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,grass_obj)) && (key_jump)
{
    vsp = -10;
    
}

//horizontal collision
if (place_meeting(x+hsp,y,grass_obj))
{
while (!place_meeting(x+sign(hsp),y,grass_obj))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;


//vertical collision
if (place_meeting(x,y+vsp,grass_obj))
{
while (!place_meeting(x,y+sign(vsp),grass_obj))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;



//animation
{
    if (hsp == 8) and key_right = keyboard_check(vk_right)
    {
        sprite_index = sPlayerWR;
        image_xscale = sign(hsp)
    }
    
    if (hsp == -8) and key_left = keyboard_check(vk_left)
    {
        sprite_index = sPlayerWR;
        image_xscale = sign(hsp)
    }
    
    if (hsp == 0)
    {
        sprite_index = sPlayerR
    }
    
}

if (key_attack)
{
    state = PLAYERSTATE.ATTACK_SLASH;
}

if hp = 0
then
{
    game_restart()
}
PLAYERSTATE_ATTACK
Code:
hsp = 0;
vsp = 0;

var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,grass_obj)) && (key_jump)
{
    vsp = -10;
    
}

//horizontal collision
if (place_meeting(x+hsp,y,grass_obj))
{
while (!place_meeting(x+sign(hsp),y,grass_obj))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;

//vertical collision
if (place_meeting(x,y+vsp,grass_obj))
{
    while (!place_meeting(x,y+sign(vsp),grass_obj))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;


//start of the attack
if (sprite_index != sAttack_slash)
{
    sprite_index = sAttack_slash;
    image_index = 0;
    ds_list_clear(hitbyattack);
    
}

mask_index = sAttack_slashHB;
var hitByAttackNow = ds_list_create();
var hits = instance_place_list(x,y,Enemy,hitByAttackNow,false);
if (hits > 0)
{
    for (var i = 0; i < hits; i++)
    {
        var hitID = hitByAttackNow[| i]
        if (ds_list_find_index(hitbyattack,hitID) == -1)
        {
            ds_list_add(hitbyattack,hitID);
            with (hitID)
            {
                EnemyHit(1);
            }   
            
            
        }
    }


}
ds_list_destroy(hitByAttackNow);
mask_index = sPlayerR;
if (animation_end())
{
    sprite_index = sPlayerR;
    state = PLAYERSTATE.FREE;

}
PLAYERSTATE_HIT
Code:
if place_meeting(x,y,sVantoid_stomperA)
{
    hp = hp - 1;
    flash = 3;
  
}

var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,grass_obj)) && (key_jump)
{
    vsp = -10;
  
}

//horizontal collision
if (place_meeting(x+hsp,y,grass_obj))
{
while (!place_meeting(x+sign(hsp),y,grass_obj))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;

//vertical collision
if (place_meeting(x,y+vsp,grass_obj))
{
while (!place_meeting(x,y+sign(vsp),grass_obj))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

//animation
{
    if (hsp == 8) and key_right = keyboard_check(vk_right)
    {
        sprite_index = sPlayerWR;
        image_xscale = sign(hsp)
    }
  
    if (hsp == -8) and key_left = keyboard_check(vk_left)
    {
        sprite_index = sPlayerWR;
        image_xscale = sign(hsp)
    }
  
    if (hsp == 0)
    {
        sprite_index = sPlayerR
    }
  
}

if (key_attack)
{
    state = PLAYERSTATE.ATTACK_SLASH;
}
PLAYERHIT
Code:
var _damage = argument0;

hp -= _damage;
if (hp > 0)
{
    state = PLAYERSTATE.HIT;
    
}
else
{
    game_restart()
}

var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,grass_obj)) && (key_jump)
{
    vsp = -10;
    
}

//horizontal collision
if (place_meeting(x+hsp,y,grass_obj))
{
while (!place_meeting(x+sign(hsp),y,grass_obj))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;

//vertical collision
if (place_meeting(x,y+vsp,grass_obj))
{
while (!place_meeting(x,y+sign(vsp),grass_obj))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

//animation
{
    if (hsp == 8) and key_right = keyboard_check(vk_right)
    {
        sprite_index = sPlayerWR;
        image_xscale = sign(hsp)
    }
    
    if (hsp == -8) and key_left = keyboard_check(vk_left)
    {
        sprite_index = sPlayerWR;
        image_xscale = sign(hsp)
    }
    
    if (hsp == 0)
    {
        sprite_index = sPlayerR
    }
    
}

if (key_attack)
{
    state = PLAYERSTATE.ATTACK_SLASH;
}
ENEMY

CREATE
Code:
hsp = 0;
vsp = 0;
grv = 0.4;
walksp = 2;

hp = 4;
flash = 0;

state = ENEMYSTATE.FREE
hitbyattack = ds_list_create();
can_attack = 0
STEP
Code:
vsp+= grv;

switch (state)

{
    case ENEMYSTATE.FREE: EnemyState_Free(); break;
    case ENEMYSTATE.HIT: EnemyState_Hit(); break;
    case ENEMYSTATE.DEAD: EnemyState_Dead(); break;
    case ENEMYSTATE.ATTACK: EnemyState_Attack(); break;


}

var dir

if (playerX_obj.x = x) dir = 0
if (playerX_obj.x < x) dir = -1
if (playerX_obj.x > x) dir = 1

if (instance_exists(playerX_obj))
    
    {

        if (dir != 0)
    {
        // add hsp to 1 or -1 x acceleration in direction
        hsp += dir* walksp;
        hsp = clamp(hsp, -walksp, walksp);
    }
    }





//Wall and ground collisions
//this comes before to see if it hits a wall or ground

if place_meeting(x+hsp, y, grass_obj)
{
    // checks to see if it has contact with the wall, if not move over 1 pixel
    // sign : function it used to check is hsp is positive or negative 1 or -1
    while !place_meeting(x+sign(hsp), y, grass_obj)
        {
            x += sign(hsp);
            
        }
        hsp = 0;
    
}

    




if place_meeting(x+hsp, y, grass_obj)
{
    // checks to see if it has contact with the wall, if not move over 1 pixel
    // sign : function it used to check is hsp is positive or negative 1 or -1
    while !place_meeting(x+sign(hsp), y, grass_obj)
        {
            x += sign(hsp);
            
        }
        hsp = 0;
    
}



//Adds current horizontal speed to x position to create movement
x += hsp;


if place_meeting(x,y+vsp, grass_obj)
{
    
    while !place_meeting(x, y+sign(vsp), grass_obj)
    {
        y += sign(vsp);
    }
    
    vsp = 0
}   

//Adds current vertical speed to y position for jumping and falling
y += vsp;

var dis = (x - playerX_obj.x);       
if (dis > 12) dir = -1;
else if (dis < -12) dir = 1;

vsp = vsp + grv;


{
    if (hsp == 2)
    {
        sprite_index = sVantoid_stomperW;
        image_xscale = sign(hsp)
    }
    
    if (hsp == -2)
    {
        sprite_index = sVantoid_stomperW;
        image_xscale = sign(hsp)
    }
    
    if (hsp == 0)
    {
        sprite_index = sVantoid_stomper
    }
    
}

if place_meeting(x,y, playerX_obj) and can_attack = 0
{
state = ENEMYSTATE.ATTACK
can_attack = 1
alarm[0] = 5*room_speed
}
ALARM 0
Code:
can_attack = 0
ENEMYSTATE_FREE
Code:
vsp = vsp + grv;

//horizontal collision
if (place_meeting(x+hsp,y,grass_obj))
{
while (!place_meeting(x+sign(hsp),y,grass_obj))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;

//vertical collision
if (place_meeting(x,y+vsp,grass_obj))
{
while (!place_meeting(x,y+sign(vsp),grass_obj))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

if hp = 0
then
{
    instance_destroy(Enemy)
}
ENEMYSTATE_HIT
Code:
if place_meeting(x,y,sAttack_slashHB)
{
    hp = hp - 1;
    flash = 3;
    
}
ENEMYSTATE_DEAD
Code:
if hp = 0
{
    instance_destroy(other)
}
ENEMYSTATE_ATTACK
Code:
//horizontal collision
if (place_meeting(x+hsp,y,grass_obj))
{
while (!place_meeting(x+sign(hsp),y,grass_obj))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;

//vertical collision
if (place_meeting(x,y+vsp,grass_obj))
{
while (!place_meeting(x,y+sign(vsp),grass_obj))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;




//start of the attack
if (sprite_index != sVantoid_stomperBITE)
{
    sprite_index = sVantoid_stomperBITE;
    image_index = 0;
    ds_list_clear(hitbyattack);
    
}

mask_index = sVantoid_stomperA;
var hitByAttackNow = ds_list_create();
var hits = instance_place_list(x,y,playerX_obj,hitByAttackNow,false);
if (hits > 0)
{
    for (var i = 0; i < hits; i++)
    {
        var hitID = hitByAttackNow[| i]
        if (ds_list_find_index(hitbyattack,hitID) == -1)
        {
            ds_list_add(hitbyattack,hitID);
            with (hitID)
            {
                PlayerHit(1);
            }   
            
            
        }
    }


}
ds_list_destroy(hitByAttackNow);
mask_index = sVantoid_stomperA;
if (animation_end())
{
    sprite_index = sVantoid_stomper;
    state = ENEMYSTATE.FREE;

}
ENEMYHIT
Code:
var _damage = argument0;

hp -= _damage;
if (hp > 0)
{
    state = ENEMYSTATE.HIT;
    
}
else
{
    state = ENEMYSTATE.DEAD;
}
 

TheouAegis

Member
Does the rest of the game still run, or is it frozen stuck in a loop? are the enemy and player still in the correct States, or are they in one of the states which cannot move? Are either the player or the enemy flag as solid? That last one shouldn't be an issue but I always check it.
 
X

xilian7125

Guest
the game is running, but the objects are in a loop
the enemy is stuck in the attack state the player is in the free state but is stuck in the walking animation
i tried to turn off the solid option but nothing changed
also the enemy does still attack but the attack never ends because the player gets stuck
EDIT: just noticed the player isn't stuck in the enemy but instead he's stuck in the attack hitbox
 

TheouAegis

Member
First off, don't use instance_destroy(Enemy). Just use instance_destroy() so you only target the instance actually running the code.

Second, you run each script from the Step event. The keyword other has no use in the Step event typically. So there's no point in using it in your EnemyState_Dead.

Additionally, there's no point checking enemy's hp inside the script EnemyState_dead, since you wouldn't be running that script if hp wasn't already 0 or lower.

Your Step event is adding grv to vsp in the enemy, which is doubling the gravity for the enemy. You didn't do that for the player, so only the enemy is wrong there.

Also, it looks to me like the player is calling EnemyHit() to deal 1 damage to the enemy, but if that doesn't kill the enemy, then it tells the enemy to set its state to EnemyState_hit, which in turn takes an additional 1 off hp. So the player would be doing 2 damage with each attack.

What's the code inside your animation_end() script?


Make sure you did not accidentally assign grass_obj as a parent to anything.
 
X

xilian7125

Guest
animation_end
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);
i honestly cant think of anything that could be causing this
 
X

xilian7125

Guest
video of problems. (me spinning is me saying its a problem.)
 
Top