0

p_hp is health variable and o_skeleton is our enemy. What I want to do is to kill the player when it collides with the skeleton 3 times, but it seems It doesn't work.

What did I do wrong?

p_hp=3;

if(place_meeting(x,y,o_skeleton))

{ 

p_hp=p_hp-1

}

if(place_meeting(x,y,o_skeleton)) && (p_hp==0)

{ 

instance_destroy(self);

}

Please help to solve my issue.

Ashish Karn
  • 1,127
  • 1
  • 9
  • 20

1 Answers1

0

Is p_hp = 3 declared in the Step Event? then that means each time it reached that code, p_hp will reset back to 3. And the Step Event is called each frame. I recommend declaring variables that'll change later on in the Create Event.

Also, It's better to use this to check if your character is still alive:

if (p_hp <= 0)
{ 
    instance_destroy(self);
}

That way it doesn't need to collide to check if it's still alive, and if the chance happens that p_hp is lower than 0. It will still be destroyed.

Keep in mind, this possible results in the player dying instantly to the skeleton, because it's checking the Step Event each frame. In that case, you'll need to look into a short invincibility time after getting hit.

Adding invincibility after getting hit:
There are multiple ways to add invincibility, the method I use is to add invincibility would be making an invincibility variable and use that as a timer. give it a value the moment it's hit, and let it return to 0 over time. you should also add the check if the invincibility value is higher than 0;

So, in practise:

Create Event:

p_hp = 3;
invicibility = 0;

Step Event:

if (invincibility > 0) //timer in step event
{
    invincibility -= 1/room_speed // (1/room_speed) = real time seconds
}

if (place_meeting(x,y,o_skeleton) && invincibility <= 0) //avoids getting hit while invincibility has value.
{ 
    p_hp -= 1;
    invincibility = 0.5; //grants half a second of invincibility after getting hit
}

if (p_hp <= 0)
{ 
    instance_destroy(self);
}

(And as extra tip: p_hp -= 1 does the same as p_hp = p_hp - 1)

Steven
  • 1,996
  • 3
  • 22
  • 33
  • Thanks this helped me a lot solving issue.But now It destroy my characters at first collision,how can I make the invincibility thing you say? – Music Is Life Jul 07 '20 at 08:29
  • Personally, I make a timer to use invincibility. I've expanded my answer to show an example. – Steven Jul 07 '20 at 12:10