0

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!

Dairo
  • 822
  • 1
  • 9
  • 22
Kavaeric
  • 1
  • 1

1 Answers1

1

You need to make some decisions about how you want the "tracking" to operate, i.e.:

  • Is it perfect (does the missile point immediately to the target, or does it take time to turn?)
  • Does the missile have a speed limit, acceleration, or a constant speed?

The simplest way to do this is likely by adjusting the angle of the missile to be that of the vector between it and the target: angle = atan2(target.y-missile.y, target.x-missile.x for example. (Forgive me if my AS2 syntax is not correct, it's been a while.)

The next step is to advance the missile's position. If you already have the angle worked out from the above, you just need to use some trigonometry to pick apart the orthogonal components:

missile.x += missile.speed * cos(angle);
missile.y += missile.speed * sin(angle);

You are using the x,y components of the vector between the missile and the target to move at a constant velocity (missile.speed) per frame. From there you could start adding things like acceleration (both for linear and angular velocities.)

Hopefully this is helpful.

Kyle Travis
  • 249
  • 1
  • 4
  • I'm assuming you mean: this._rotation = Math.atan2(_root.targetY._y, _root.targetX._x); this._x += projSpeed * Math.cos(this._rotation); this._y += projSpeed * Math.sin(this._rotation); And you meant the missile's rotation when you said "angle", "missile.speed" meant an assigned variable for the missile's speed, and "target.y-missile.y" means the target's Y coordinate? – Kavaeric Oct 24 '13 at 05:27
  • Make sure you're taking the arctangent of the *difference* of the position between the target and the missile, otherwise you will get some weird results. Other than that, yes! – Kyle Travis Oct 24 '13 at 05:39