I've searched everywhere and found a few resources in coding homing missiles, however with me being kind of new to actionscript and coding in general I can't really understand what the different bits do, let alone incorporate them in my game.
I'm making a scrolling shooter game for a school project. There's a special "tertiary" weapon that works as follows:
- The weapon auto-fires when an enemy is in range
- The weapon targets the closest enemy and goes towards it
- It's a missile too, so it needs that rotary movement too
- When the target is destroyed the missile will self-destruct
I already have the code for finding out the nearest target:
for (var i in enemies)
{
var min = Infinity; // Set to infinity so the if statement will work
var tempDist = getDistance(enemies[i]); // tempDist stores the distance
if (tempDist < min && tempDist < 300)
{
min = tempDist;
closestEnemy = enemies[i];
}
}
(getDistance() is a function for finding the distance between two MCs in my code)
What I want it to do is have closestEnemy store the identifier or "undefined" if there is no nearby target. The homing missile MC has a class that should constantly update the position of the closest enemy through _root.playership.closestEnemy. If it's "undefined", it should explode.
The player's spaceship contains an array called "enemies" that contains all the identities of all spawned enemies. I have the reload and firing rate systems good to go, it's just the tracking that is the problem.
Any help would be appreciated, thanks!