Free ActionScript
Home
AS3 Scripts
AS2 Scripts
Resources
About
Contact
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
Update: ActionScript 3 version of this script is available here: http://www.freeactionscript.com/2011/07/game-enemy-manager/
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?
pradvan March 11, 2011 at 2:06 pm Hi Dommos, Please upload your fla somewhere and post a link. I’ll take a look -PR Log in to Reply Dommos March 12, 2011 at 10:47 am 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) Log in to Reply pradvan March 12, 2011 at 3:00 pm 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 Log in to Reply pradvan March 12, 2011 at 3:03 pm 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. Log in to Reply
Hi Dommos,
Please upload your fla somewhere and post a link. I’ll take a look
Dommos March 12, 2011 at 10:47 am 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) Log in to Reply pradvan March 12, 2011 at 3:00 pm 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 Log in to Reply pradvan March 12, 2011 at 3:03 pm 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. Log in to Reply
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)
pradvan March 12, 2011 at 3:00 pm 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 Log in to Reply pradvan March 12, 2011 at 3:03 pm 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. Log in to Reply
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.
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!
You must be logged in to post a comment.
AS3 Effects
AS3 Gadgets
Game Examples AS2 Effects Animated Effects Dynamic Enemy AI Game Engine Game Weapons Images Interface Math Security
Effects Animated
Effects Dynamic
Enemy AI
Game Engine
Game Weapons
Images
Interface
Math
Security
Game Examples AS3 Basics Bitmap Blitting Collision Detection Enemy AI Movement Physics Space Shooter User Interface Weapon System
Basics
Bitmap Blitting
Collision Detection
Movement
Physics
Space Shooter
User Interface
Weapon System
Site Updates
718Hosting.com
Actionscript.org Forums
Flash Game Blogs
Flash Game License
Flashkit Forums
gotoAndLearn
IndieGames.com
Kirupa’s Tutorials
Kongregate Forums
MochiAds Resources
Philip Radvan
Photon Storm
Polygonal
Radvan Media
Tonypa’s Tutorials
Register
Log in
Entries RSS
Comments RSS
WordPress.org