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

How do you make it so your character is invincible for a second after taking damage

flerpyderp

Member
Before damaging the player, check to see if invincible is set to false. If it is, set it to true and take away player HP. Have an else statement (or a seperate if statement elsewhere in the code, depends on how you're implementing damage) with a timer count down, once it reaches 0 set invincible back to true and reset the timer.
 
8

82499

Guest
I'm not sure how to set invincibility and I'm running GM 8.1 Lite
 

flerpyderp

Member
I'm not sure how to set invincibility and I'm running GM 8.1 Lite
Make a variable in the create event called "invincible", set it to false. The idea is that the player will only take damage while it is false, so each time they do take damage it is immediately set to true. Then you simply check that invincible is set to true, and if so, start a timer. Once it reaches 0, invincible is set back to false, allowing the player to be damaged again.
 
8

82499

Guest
ok, thanks, but how do you code the part where it only takes damage when it is false?
 
8

82499

Guest
also, how do you make the sprite flash while it is invincible(like mario) so the player knows it cant take damage
 
From your questions, it might be worth it for you to run through a few of the tutorials, in GMS 2 there are several you can access from the Start Page.

They will answer a lot of your questions and build some of your coding knowledge where you will be able to answer these things yourself.

ok, thanks, but how do you code the part where it only takes damage when it is false?
In the collision event, you would have some code that checks if invincible is true or false. If it is false, then take damage.

For example:
Player Collision Event with Enemy
Code:
if ( !invincible )
{
    hp -= 1;
}
also, how do you make the sprite flash while it is invincible(like mario) so the player knows it cant take damage
Instances have several built-in properties you can manipulate. You can set what is known as the sprite_index to whatever sprite you wish in order to change the appearance of the instance.

For example, you can make one player sprite which is normal coloured and used by default.

You can create a second sprite with the same shape as the first sprite, but colour it all completely as white.

Then, when your player gets hit, you change the sprite to the white coloured sprite for a few steps, before changing it back to the normal one.

Player Collision with Enemy Event:
Code:
if ( !invincible )
{
    hp -= 1;
    sprite_index = spr_player_hit; // Change the sprite to the "hit" sprite.
    alarm[0] = 5; // Set an alarm which will activate after 5 steps.
}
Player Event Alarm 0
Code:
sprite_index = spr_player_normal;//Change the player sprite back to the normal one.
 
Top