Search code examples
game-maker

Game maker- How to add a period of invulnerability between collisions


Completely new to game-maker and coding, trying to teach myself gml through following tutorials and what not. Anyway I followed a platformer enemy AI tutorial in; which when you hit the enemy object and are not above it the game will restart. I changed this to a line where the player loses 10 health as I wanted to add a health system into my game. However when the player comes into contact with the enemy, the player loses all of its health as the collision is constantly ticking. Anyway I was wondering how to add a few seconds in between each collision where the player is invulnerable? Thank you in advance Below is the collision event code that I'm using:

if (y < other.y-vspd) {
with (other) {
instance_destroy();
}
vspd = -jspd;
} else {
global.playerhealth -= 10;
}

Solution

  • Make a boolean such as invulnerable for the player's create event and set it to false. Then, add this code in the player's step event:

    if (invulnerable){
        if (time < frames * seconds){
            time++;
        } else{
            invulnerable = false;
        }
    }
    

    Change frames to the frames-per-second/room_speed your game is on (defualt is 30).

    Change seconds to the amount of seconds you want the player to be invulnerable.

    Then you can change the code you have to:

    if (y < other.y-vspd) {
    with (other) {
    instance_destroy();
    }
    vspd = -jspd;
    } else if (!invulnerable) {
    global.playerhealth -= 10;
    invulnerable = true;
    }