Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-31-2009, 02:38 PM   #1
12Hits
Registered User
 
Join Date: Aug 2009
Posts: 6
Default [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
12Hits is offline   Reply With Quote
Old 08-31-2009, 05:53 PM   #2
12Hits
Registered User
 
Join Date: Aug 2009
Posts: 6
Default

Anybody please?
12Hits is offline   Reply With Quote
Old 08-31-2009, 06:31 PM   #3
Greg SS
Uber sleepy
 
Join Date: Nov 2007
Location: Bandung
Posts: 886
Send a message via Yahoo to Greg SS
Default

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<zombies.length; i++){         delta.x = Math.abs(zombies[i]._x - x);         if(delta.x > 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]; }
Greg SS is offline   Reply With Quote
Old 08-31-2009, 07:48 PM   #4
12Hits
Registered User
 
Join Date: Aug 2009
Posts: 6
Question

Quote:
Originally Posted by Greg SS View Post
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<zombies.length; i++){         delta.x = Math.abs(zombies[i]._x - x);         if(delta.x > 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.'
12Hits is offline   Reply With Quote
Old 09-01-2009, 03:48 AM   #5
Greg SS
Uber sleepy
 
Join Date: Nov 2007
Location: Bandung
Posts: 886
Send a message via Yahoo to Greg SS
Default

add
import flash.geom.Point
and change Movieclip to MovieClip
Greg SS is offline   Reply With Quote
Old 09-01-2009, 08:35 AM   #6
12Hits
Registered User
 
Join Date: Aug 2009
Posts: 6
Default

Quote:
Originally Posted by Greg SS View Post
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.
12Hits is offline   Reply With Quote
Old 09-01-2009, 11:10 AM   #7
Greg SS
Uber sleepy
 
Join Date: Nov 2007
Location: Bandung
Posts: 886
Send a message via Yahoo to Greg SS
Default

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.
Greg SS is offline   Reply With Quote
Old 09-01-2009, 01:43 PM   #8
12Hits
Registered User
 
Join Date: Aug 2009
Posts: 6
Default

Oh right k I'll work with it. Thanks for the push in the right direction
12Hits is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Jump


All times are GMT. The time now is 04:16 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.