Question About Multiple Collisions At Once (Kinda)

So, I’m in the process of making a tower defense game. Currently the way it works is that when an enemy touches the collider of the tower, the tower locks on to that enemy and no others until that enemy is out of range or is dead. However, this system seems to prioritize enemies that were created first rather than enemies that are farther up on the track. Is there a way to compare not two, but multiple variables (these would store info on how far the enemy has travelled) so the tower could choose the enemy that has travelled the furthest instead of the enemy that was drawn the first?

Yes, your case as you explained indicates it is possible. To move forward in this discussion, you need to provide some minimum code that reproduces your observation and shows your approach.

Kf

1 Like

Okay, it’s not the cleanest or most efficient of code but this is the current code for targeting enemies:

towers[5] stores a boolean for if an enemy has been identified.
towers[6] stores the enemy sprite
towers[7] stores the “range” sprite that displays the range of the tower and is the collider for if an enemy is within range
towers[8] stores the rest of the properties of the enemy sprite

Again, I know it isn’t the best of code, but I hope you can help.

``````  for(var x = 0; x < towers.length; x++){
for(var y = 0; y < enemies.length; y++){

if(towers[x][7].isTouching(enemies[y][0]) && towers[x][5] === false){
towers[x][5] = true;
towers[x][6] = enemies[y][0];
towers[x][8] = enemies[y];
}
if(towers[x][5] === true){

towers[x][0].rotation = atan2(towers[x][6].x-towers[x][0].x, towers[x][6].y-towers[x][0].y) * -1;

if(towers[x][7].isTouching(towers[x][6]) === false){
towers[x][5] = false;
towers[x][6] = null;
}
}
}
}
``````

If there was a way for be to compare multiple numbers at once, and find which one is the highest, that would greatly help.

Wouldn’t passing your array through a sorting function solve this issue. Sorting by distance to wall.

1 Like

You would need to either sort it as described by paulgoux or loop through the data twice. The first one is to determine and select based on range and the second one will do the current task based on these results from the first loop.

Kf

1 Like

Huh, I didn’t even know a sorting function existed in javascript until you mentioned it. Thanks!

Errrr, I’m trying what paulgoux suggested and to do so I’ve created an array called distances, but when I try to push my distance I get a type error. Uh, what am I doing wrong?

``````function createNormalEnemy(){
var normalEnemy = createSprite(-50 , 100, 25, 25);
normalEnemy.addImage("normal enemy", normalEnemy_img);
normalEnemy.scale = 0.15;
distance = 0;
distances.push(distance);
enemies.push([normalEnemy, 4, 20, false, false, "normal", distance]);
}
``````

EDIT: Okay, that appears not to be the main issue. Using sort completely breaks my targeting code. My towers don’t lock onto any enemies any more. Current code below

``````      distances.sort(function(a, b){return b-a});

if(towers[x][7].isTouching(enemies[y][0]) && towers[x][5] === false && enemies[y][7] === distances[0]){
towers[x][5] = true;
towers[x][6] = enemies[y][0];
towers[x][8] = enemies[y];
}

``````

You might might need to make a copy of the array first, then the targeting system will use the original data.