Help with creating enemy attack hitbox

J

JollyLemon

Guest
Hi,

I made a simple enemy AI system using states. I'm trying to create an enemy attack hitbox for 1 frame whenever the sprite_index = spr_zombie_attack and image_index==1.
I put this in the enemy step event:
Code:
case zombie_state.attack:
    {
       if (!attacking)
       {
       attacking = true;
       sprite_index = spr_zombie_attack;
       image_speed = 0.12;
       image_index = 0;
       //Hitbox
        if (image_index==1)
       {
            with (instance_create (x,y,obj_zombie_hitbox)) //Create hitbox
            {
                image_xscale = other.image_xscale; //Create the hitbox in the right direction
            }   
       }
        movespeed = 0;
        }
        if (distance_to_object (obj_player) > 30) state = zombie_state.chase;
    }
    break;
And in obj_zombie_hitbox PostDraw event I put:
Code:
instance_destroy ();
But the hitbox never appears. Whenever the enemy is in range, the animations and all work but the hitbox never gets created :s

Anyone have any tips?
Thanks for reading BTW! :)
 

jo-thijs

Member
You first set image_index to 0.
Directly afterwards you check if image_index equals 1.
Then you ask why the if statement fails?
 
J

JollyLemon

Guest
You first set image_index to 0.
Directly afterwards you check if image_index equals 1.
Then you ask why the if statement fails?
I'm sorry, I can't follow your analogy. (I feel like a noob)
Are you asking me to move the codes around?
Or remove the image_index = 0; line?

Thanks for the fast reply BTW
 

jo-thijs

Member
It's not an analogy, I'm literally saying what your code does.
You've got this:
Code:
       image_index = 0;
       //Hitbox
        if (image_index==1)
This sets image_index to 0 and checks afterwards if it equals 1, which is impossible.

I'm guessing you want to put this code:
Code:
        if (image_index==1)
       {
            with (instance_create (x,y,obj_zombie_hitbox)) //Create hitbox
            {
                image_xscale = other.image_xscale; //Create the hitbox in the right direction
            }   
       }
        movespeed = 0;
Outside of this:
Code:
       if (!attacking)
       {
       ...
       }
       // above code would go here
However, that will still not work, as image_speed is set to 0.12, which will make image_index miss the value 1.

You'll have to check if image_index >= 1 && image_index < image_speed + 1
instead of checking if image_index == 1.
 
J

JollyLemon

Guest
It's not an analogy, I'm literally saying what your code does.
You've got this:
Code:
       image_index = 0;
       //Hitbox
        if (image_index==1)
This sets image_index to 0 and checks afterwards if it equals 1, which is impossible.

I'm guessing you want to put this code:
Code:
        if (image_index==1)
       {
            with (instance_create (x,y,obj_zombie_hitbox)) //Create hitbox
            {
                image_xscale = other.image_xscale; //Create the hitbox in the right direction
            }  
       }
        movespeed = 0;
Outside of this:
Code:
       if (!attacking)
       {
       ...
       }
       // above code would go here
However, that will still not work, as image_speed is set to 0.12, which will make image_index miss the value 1.

You'll have to check if image_index >= 1 && image_index < image_speed + 1
instead of checking if image_index == 1.
Wow! That works perfectly, thank you so much!
 
Top