Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums [AS2] Referencing closest enemy to a friendly AI
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 08-31-2009, 02:38 PM #1 12Hits Registered User   Join Date: Aug 2009 Posts: 6 [AS2] Referencing closest enemy to a friendly AI What's up everyone, how's it going. Got a bit of a problem I need sorting so here goes.. Basically, my game spawns various enemies dynamically (using a random script). These all use the instance name "zombie"+enemyvar so they can be attached at runtime. Now I need to know how I can calculate the enemy which is closest to my friendly AI person (x and y distance), and reference that one in the friendly's actionscript (ie. _root.somethingsomething). All help is highly appreciated, hope I've written this clearly enough
 08-31-2009, 05:53 PM #2 12Hits Registered User   Join Date: Aug 2009 Posts: 6 Anybody please?
 08-31-2009, 06:31 PM #3 Greg SS Uber sleepy   Join Date: Nov 2007 Location: Bandung Posts: 886 ActionScript Code: ```function nearestZombie(x:Number, y:Number):Movieclip {     var min:Number = Number.MAX_VALUE;     var dist:Number, index:Number;     var delta:Point = new Point();     for(i=0; i min) continue;         delta.y = Math.abs(zombies[i]._y - y);         if(delta.y > min) continue;         dist = delta.length;         if(dist > min) continue;         min = dist;         index = i;     }     return zombies[i]; }```
08-31-2009, 07:48 PM   #4
12Hits
Registered User

Join Date: Aug 2009
Posts: 6

Quote:
 Originally Posted by Greg SS ActionScript Code: ```function nearestZombie(x:Number, y:Number):Movieclip {     var min:Number = Number.MAX_VALUE;     var dist:Number, index:Number;     var delta:Point = new Point();     for(i=0; i min) continue;         delta.y = Math.abs(zombies[i]._y - y);         if(delta.y > min) continue;         dist = delta.length;         if(dist > min) continue;         min = dist;         index = i;     }     return zombies[i]; }```
Got some errors when I tried this..

'The class or interface 'Movieclip' could not be loaded.'
'The class or interface 'Point' could not be loaded.'

 09-01-2009, 03:48 AM #5 Greg SS Uber sleepy   Join Date: Nov 2007 Location: Bandung Posts: 886 add import flash.geom.Point and change Movieclip to MovieClip
09-01-2009, 08:35 AM   #6
12Hits
Registered User

Join Date: Aug 2009
Posts: 6

Quote:
 Originally Posted by Greg SS add import flash.geom.Point and change Movieclip to MovieClip
It still doesn't seem to be working unfortunately..

So, let me get this straight - can I use this code on a Movie Clip's actions (inside an onClipEvent(enterFrame))?

Do I need to change any of the variables in the script like zombies or i? Because at present it doesn't look like the script actually references the instance names of my zombies.

I am calling the function, but if I try and trace zombies[i] right after, I constantly get undefined.

 09-01-2009, 11:10 AM #7 Greg SS Uber sleepy   Join Date: Nov 2007 Location: Bandung Posts: 886 I'm only giving you one of the concept of how it can be done. I don't know how you set your game, you would have to adapt the code to your game yourself. __________________ My help efforts are postcardware. PM me for my local address to send your postcard if you're kind enough to indulge my weird hobby.
 09-01-2009, 01:43 PM #8 12Hits Registered User   Join Date: Aug 2009 Posts: 6 Oh right k I'll work with it. Thanks for the push in the right direction

 Thread Tools Display Modes Rate This Thread Hybrid Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 07:20 AM.

///