Target Closest Enemy MovieClip
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
5 Comments to Target Closest Enemy MovieClip
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
January 22, 2009
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.
January 27, 2009
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.
May 24, 2010
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);
Leave a comment
You must be logged in to post a comment.
CategoriesLinks |

January 22, 2009