Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

Target Closest Enemy MovieClip

15 Comments »

I’ve been thinking about how targeting the closest enemy would work and started experimenting with the algorithm. Basically AS has an array method called sortOn, that sorts objects properties in any order you want. Well, that’s the catch. It only sorts objects, not movieClips.

The work around is simple enough; when creating a new enemy object, just save a reference to the movieClip on stage inside the object’s property.

Preview

Download Fla Sample

Download Fla Sample

Update:
ActionScript 3 version of this script is available here:
http://www.freeactionscript.com/2011/07/game-enemy-manager/

15 Responses

Thank you so much, I tried for several hours while creating enemies in an array, and i could adjust their x and y manually but the rocket would still go after the “newest” one. your amazing. <3

  • Hey, as I could find no way of contacting you… How would you make a “continuous laser”? Like:

    When you shoot, your laser EXTENDS from where you are forwards, instead of just appearing at full length like your laser tutorials do. Also, when the laser hits an enemy, only part of the laser disappears, looking like the enemy is “blocking” the laser. Also, after you stop shooting your laser, it still progresses forwards, instead of disappearing immediately… I can’t really describe it clearly in words lol.

  • for the life of me, i still cannot get the missle’s to shoot at the closest enemy when the enemies are not created at the same time you shoot. (click:shoot, space:creat enemies)

  • I’ve been pretty busy so I apologize for the delayed reply.

    imoncloudnine69: I will make a missile example that shoots the closest enemy and post it up.

    jonathanasdf: I think I know what you’re talking about. Something like that would require trigonometry. I’ll keep that in mind when I rework the laser example.

  • i believe you can take the sortOn and the gotoAndStop outside of the for loop

    for(var i = 0; i < enemyObjectsArray.length; i++)
    {
    //set distances
    }
    enemyObjectsArray.sortOn("distanceTotal", Array.NUMERIC);
    enemyObjectsArray[0].myMc.gotoAndStop(2);

  • I need to have the same effect in my game as you have here, where the nearest enemy is targeted, but i’m using AS3.

    Please help.

  • gramthebassist,

    AS2 to AS3 math is almost exactly the same. Which part are you having trouble with?

  • Hi, i am using this code but my missile always targets _level0.Enemy3

    can you help?

    • Hi Dommos,

      Please upload your fla somewhere and post a link. I’ll take a look

      -PR

      • Hi, i have managed to get past this error, but now, every time a missile hits an enemy (if i have “removeMovieClip(Target)” in the doFollow function), every other missiles stop. I am using my own enemy AI (that i will improve)

        http://www.mediafire.com/?gig8ti3zikwe011 (FLA file)

        • Dommos,

          You are setting a missile’s target when you create the missile. If a different missile hits the same target (and the target gets removed), all the other missiles stop because they don’t have a target.

          Add conditional (if statement) that checks if a missle’s current target is null/undefined, get a new target.

          -PR

        • Also, I would suggest not to add an enterFrame to each missile, but rather push each new missile in to a missile array, and update that missile array with one enterFrame.

          Same principle as the create new enemy/update enemy functions.

  • never mind, all i did was (mistaking split and splice)

    enemyObjectsArray[0].myMc.removeMovieClip();
    enemyObjectsArray.splice(0, 1);

    in the doFollow function and it sorted it out. thanks any ways. PS i checked if it’s target was null or undefined, and if it was, to set a new target. this did not work. it is sorted out now though, thankyou.

  • Oh, and a note for your code:
    tempEnemy_obj = {myMc:tempEnemy_mc, distanceTotal:0, distanceY:0, distanceY:0}

    should be

    tempEnemy_obj = {myMc:tempEnemy_mc, distanceTotal:0, distanceX:0, distanceY:0}

    you had 2 distanceY’s

  • Hi Dimmos,

    Glad you were able to figure it out.

    Thanks for catching that error!

    -PR

  • Leave a Reply

    You must be logged in to post a comment.