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

place_meeting trouble

D

DictatorPanda

Guest
I'm making a basic space shooter arcade game and instead of using the built-in collision event I'm using place_meeting. Since this would have to be in the step event and the step event runs every frame I made a boolean variable named hit that will become true once an enemy hits the player's ship.

if (place_meeting(x,y, obj_enemy))
{
hit = true;
if (hit == true)
{
global.player_lives -=1;
hit = false;
}
}

The player is still dying in one hit. Can someone explain to me why, thank you.
 

OliverSB

Member
This code instantly sets hit to true then checks if it i true which it is so therefore every step this will decreases player lives by 1 and set hit back to false only for it to be set back to true next time you collide with the enemy.

The hit boolean check seems reduntant because its always true when checking.
 

OliverSB

Member
Are you trying to get the enemy to only do damage on the first step that it touches the enemy only then when it re-collides do more damage?
 

FrostyCat

Redemption Seeker
You should read my article on how to use flags. The variable should be set up from another event, not right before the flag check.

Create:
Code:
hit = true;
Step:
Code:
if (place_meeting(x, y, obj_enemy)) {
    if (hit) {
        global.player_lives -= 1;
        hit = false;
    }
} else {
    hit = true;
}
 

Yal

šŸ§ *penguin noises*
GMC Elder
Let's take a closer look at the code.
Code:
hit = true;
if (hit == true)
You're setting the flag right before checking if it's set. Can it ever not be set when checking if it's set, then?
 
Top