Author Topic: Enemy in FlxGroup shooting at nearest enemy in another FlxGroup? [Solved]  (Read 1234 times)

VictorGrunn

  • Member
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Still learning some fundamentals of AS3 here.

I'd like to do something like the following: Have two FlxGroups filled with FlxSprites (say, enemygroup1 and enemygroup2). I'd like to make it so, if there's multiple of both groups on the screen (3 sprites in enemygroup1 and 3 in enemygroup2), enemygroup1 will shoot at the nearest memeber of enemygroup2.

Anyone got a good example of how to pull this off?
« Last Edit: Wed, Apr 18, 2012 by VictorGrunn »

VictorGrunn

  • Member
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Just to show I'm not totally slacking here.

I'm imagining a solution would look something like this.

Put code in the FlxSprite to reference the FlxGroup in question, and run code going through all the members (say enemygroup1.length), calculating the distance from the FlxSprite to all the members of the group with exists == true. Then choosing the one with the shortest distance away to fire at.

Just wondering if there's a 'better' way.

VictorGrunn

  • Member
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Alright. Currently wrestling with a problem related to this.

Here's the function I'm using.

Code: [Select]
private function checkDistance():void
{

var estimate:uint = 20000;
var closest:uint = 20000;
var whichIsClosest:uint = 0;

var dx:uint = 0;
var dy:uint = 0;
trace("Members in group: " + String(Registry.enemyManager.enemiesGroup.members.length));
for (var i:int = 0; i < Registry.enemyManager.enemiesGroup.members.length; i++)
{
if (Registry.enemyManager.enemiesGroup.members[i] != null && Registry.enemyManager.enemiesGroup.members[i] != undefined &&
Registry.enemyManager.enemiesGroup.members[i].exists == true)
{
dx = crosshairTarget.x - Registry.enemyManager.enemiesGroup.members[i].x;
dy = crosshairTarget.y - Registry.enemyManager.enemiesGroup.members[i].y;
estimate = Math.sqrt(dx * dx + dy * dy);
trace("Target " + i + " estimate: " + String(estimate));

if (estimate <= closest && Registry.enemyManager.enemiesGroup.members[i].exists == true && Registry.enemyManager.enemiesGroup.members[i].health >= 1)
{
closest = estimate;
whichIsClosest = i;
trace(whichIsClosest + " was closest.");
}
}
}

trace("Closest: " + String(closest) + "\nWhich one we shot: " + whichIsClosest);
if (Registry.enemyManager.enemiesGroup.members[whichIsClosest] != null)
{
Registry.enemyManager.enemiesGroup.members[whichIsClosest].hurt(100);
}
}

I'm getting a few oddities with this.

1) Traces like this:

Quote
Members in group: 4
Target 0 estimate: 149
0 was closest.
Target 1 estimate: 202
Target 2 estimate: 4294967248
Target 3 estimate: 4294967017
Closest: 149
Which one we shot: 0

I don't know why 2 and 3 are turning up the way they are.

And 2) Probably related to the above results, now and then this command does not blow up the target closest to the crosshair.

Any advice?

Edit: I had a hunch that using type uint was a problem here. Switched to type Number, problems went away.
« Last Edit: Wed, Apr 18, 2012 by VictorGrunn »