Checking if hit (Solved)

bandman28

Member
I have a bomb exploding, and enemies in it's range.
I want the bomb to do damage only once to each enemy so that it doesn't keep subtracting from the enemies health.
I wrote this:
(this is in the bomb)
GML:
//Create
damage = .5;
hit = [];
GML:
//Collision with enemy
var not_hit = true;
for (var i = 0; i < array_length(hit); i++) {
    if (hit[i] = other) {
        not_hit = false;
    }
}
if (not_hit) {
    other.hlth -= damage;
    hit[array_length(hit)] = other;
}
I also have code that destroys the bomb after exploding, so the array doesn't do anything else.
But when I play the game, the bomb doesn't show signs of doing any damage.
Is there any way to keep track of what enemies this bomb has hit, so that it does not repeatedly take damage from the enemy?
 

Simon Gust

Member
A single time event is one of the easier scenarios for this problem.
The bomb can be destroyed on detonation and not hit anything else after that.
A with() statement loops through every object of choice.

collision with enemy
GML:
// create local variables, so you do not need "other".
var bomb_x = x;
var bomb_y = y;
var bomb_damage = damage;

// check every enemy
with (obj_enemy)
{
    // check if enemy is in range
    if (point_distance(bomb_x, bomb_y, x, y) < 100) 
    {
        // deal damage to enemy
        hlth = max(0, hlth - bomb_damage);
    }
}

// destroy bomb
instance_destroy();
 
Top