GameMaker Attack animation not working correctly

OnLashoc

Member
Ok this is a head scratcher..

So I am having problems with this animation working correctly. I have two different attack animations with same code, one is working, the second is not with the same exact coding. One is of my oPunch animation which isn't working, it has the following code on the Step event:

Code:
/// @description

if (sprite_index == sRpunch && image_index = image_number -11)
    {
    audio_play_sound(snWoosh1,1,0);
    }
   
//audio_play_sound(snRHook,0,false);
    if instance_exists(oEnMaddog) && image_index == 4
    {
    audio_play_sound(snRHook,0,false);
    oEnemyhealth.maddogHealth -= 50;
    }
The second attack animation is a hand with an axe in is called oAxehandswing it is working correctly and has the following code on the Step event:

Code:
/// @description

if (sprite_index == sAxeswing1 && image_index = image_number -6)
    {
    audio_play_sound(snWoosh1,1,0);
    }
   
//audio_play_sound(snRHook,0,false);
    if instance_exists(oEnMaddog) && image_index == 4
    {
    audio_play_sound(snRHook,0,false);
    oEnemyhealth.maddogHealth -= 50;
    }

The sAxeswing1 animation has 8 frames.
The sRpunch animation has 11 frames.

Why will the Axe animation work and do damage to Mr. Hotdog Man but the Punch animation not do damage?

I am baffled, any help would be appreciated.

Onnie
 

Kyrieru

Member
Assuming you're using image speed, it may not add up to the number you're checking for. (for example a speed of 0.3 will not ever equal 1, but will equal 3)
Also, any reason you're doing image_number -11 if the animation has 11 frames?
 

Relic

Member
To explain in a little more detail, image_index is not necessarily an integer. If image_index is 0, and your sprite has a speed of 13 frames per second in the sprite editor in a game set to run at 60 frames per second (frames here should read as “steps”, not sprite frames) then each game step, image_index will increase by 13/60=0.2167.

The chances of image_index being equal to any exact integer after X number of steps is slim. It’s standard practise to use a range of acceptable values- if image_index>=8 or to round the value of image_index- if floor(image_index)=8.
 

OnLashoc

Member
Assuming you're using image speed, it may not add up to the number you're checking for. (for example a speed of 0.3 will not ever equal 1, but will equal 3)
Also, any reason you're doing image_number -11 if the animation has 11 frames?
Maybe my understanding is incorrect, by why wouldn't I? I wanted the woosh sound at the middle to end of the animation. Setting it to 11 achieved the desired effect at the correct timing with the woosh sound wave I am using. The damage should be happening on frame 4 where the fist makes contact with the enemy face / mid-section. That's the part where I'm having problem. So the first half of the coding above is working (playing the woosh sound) the second half is not. It's not erroring, it's just not taking the damage away from the enemy health. If I'm missing the point of the question, please forgive me but I don't understand.

To explain in a little more detail, image_index is not necessarily an integer. If image_index is 0, and your sprite has a speed of 13 frames per second in the sprite editor in a game set to run at 60 frames per second (frames here should read as “steps”, not sprite frames) then each game step, image_index will increase by 13/60=0.2167.

The chances of image_index being equal to any exact integer after X number of steps is slim. It’s standard practise to use a range of acceptable values- if image_index>=8 or to round the value of image_index- if floor(image_index)=8.
Yes and I tried

Code:
 if instance_exists(oEnMaddog) && image_index >= 4
And it did do damage, but it did damage on every frame after frame # 4 and instantly killed Hotdog Man lol so that's a no go. I know being a non-integer could be a problem but I just don't understand how to fix. What would work wit the coding above to make it do damage appropriately? And why is the other object as stated above doing damage correctly?

Thank you for the help!
 

Kyrieru

Member
Image_number is how many frames are in the animation (In your case, 11).
So you essential wrote 11 - 11 = 0.

If you have a non integer image_speed, then you cannot reliably use something like this.
Code:
if image_index = 4
if image_index goes from 3.8 to 4.1, the check never runs it's code, because image_index was never "equal" to 4.

So, like Relic said, you have to do this instead.
Code:
if image_index >= 4
That way, it will run the code even if image_index overshoots 4.
However, because image_speed will be higher than 4 multiple times, you need to make it so the code is only executed once.
To do this, you can do something like this.

Code:
if image_index <4
can_hit = 1

if image_index >=4
if can_hit =1
{
///do stuff
can_hit = 0
}
You create a variable that checks if the attack has already hit. If it has, it doesn't run the code again.
 

OnLashoc

Member
Ahh haa thank you for detailed explanation, the both of you. I understand now, will try it like this instead and let ya know how it goes!
 
Top