Hi, i’m programming a game right now for school and i’ve got a function to spawn enemies being called by a loop to create an endless “spawn point”. It works great as it is, but I would like to make the enemies collide with eachother. here is my enemy spawn code.
enemyA = [];
var enemyCounter:Number = 0;
function createEnemy(cspeed, csprite, crange, clocx, clocy, cname)
{
//attaches the graphic for the enemy to the stage and sets all the variables
enemy = background.attachMovie([csprite], [cname], background.getNextHighestDepth()+50000, {_x:clocx, _y:clocy});
enemy.espeed = cspeed;
enemy.erange = crange;
enemy.cacheAsBitmap = true;
//adds the enemy to the array
enemyA.push(enemy);
enemyCounter++;
for (var i = 0; i<enemyA.length; i++) {
if(enemy.hitTest(enemyA*) && !enemy.hitTest(enemyA[enemyCounter]))
trace("yay");
}
this.onEnterFrame = function() {
point={x:player._x,y:player._y};
background.globalToLocal(point);
/ /loops through the enemy array to see where each enemy is in relation to the
//player and if they are too far away //it moves them towards the players position
for (var i = 0; i<enemyA.length; i++) {
if (point.x < enemyA*._x - enemyA*.erange)
enemyA*._x -= (enemyA*.espeed+Math.random()*2);
if (point.x > enemyA*._x + enemyA*.erange)
enemyA*._x += (enemyA*.espeed+Math.random()*2);
if (point.y < enemyA*._y - enemyA*.erange)
enemyA*._y -= (enemyA*.espeed+Math.random()*2);
if (point.y > enemyA*._y + enemyA*.erange)
enemyA*._y += (enemyA*.espeed+Math.random()*2);
}
}
}
Does anyone know how I could go about making them collide with eachother? I know about hitTest but the only ways i’ve been able to think of doing it result in the hitTest being true when an enemy collides with itself (always) which is not something I want.