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 05-15-2012, 09:34 AM   #1
jpoopdog
Member
 
Join Date: Feb 2012
Posts: 52
Default check if movieclip is within a circular radius of another movieclip

Hello.

I am currently making a game with weapons and zombies, no further explanation needed.

One weapon useable is a rocket, which needs to explode. when it hits something, i want it to detect if any enemies are within a circular range of the it, in which case i would trigger their death timers, which once triggered make them stop and fade away.

Ive been doing some searching and cant find anything about this, does anyone know the code, or where i can find the code?
jpoopdog is offline   Reply With Quote
Old 05-16-2012, 09:17 AM   #2
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

You need the squared length of the distance from the point your testing to the center of the circle.
Then test to see if that number is less than the circles radius squared.

In programming, when using geometry, you'll come across what's known as a Vector2D or 2DVector, it's much like the flash.geom.Point class but normally has a few more methods for subtraction, addition, multiplication, division and so forth with other points in 2D space.

Using such a class as the basis for calculations, testing for a point in a circle is easy.

ActionScript Code:
function testForPointInCircle(circlesPosition:Vector2D, circlesRadius:Number, pointInQuestion:Vector2D):Boolean { var dist:Number = pointInQuestion.subtract(circlesPosition).squaredLength; if(dist < circlesRadius * circlesRadius) {    return true; } return false; }

The trick becomes applying this to MovieClips.
One way is to create a property on the MovieClip that is an instance of a Vector2D, then when you update the Vectors x and y you also then update the MovieClips x and y.
Or even better, create a special class that takes a MovieClip as a parameter that extends a Vector2D that would update the MovieClips position at the same time its x and y get updated in a getter/setter x and y.
ActionScript Code:
private var _x:Number; public function set x(value:Number):void {    this._x = value;    this.movieClipRef.x = value; } public function get x():Number {    return this._x; } private var movieClipRef:MovieClip; public function MCVector2D(MC:MovieClip):void {     this.movieClipRef = MC; } //... for example
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...

Last edited by tadster; 05-16-2012 at 09:28 AM.
tadster is offline   Reply With Quote
Old 05-17-2012, 07:34 AM   #3
jpoopdog
Member
 
Join Date: Feb 2012
Posts: 52
Default

looks a bit complex. ill see if i can add it to the game, thanks
jpoopdog is offline   Reply With Quote
Old 05-17-2012, 04:24 PM   #4
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

Pythagorean theorem, solving for hypotenuse
For two movieclips a and b:
Code:
var dist:Number = Math.sqrt(Math.pow(a.x-b.x,2)+Math.pow(a.y-b.y,2));
For the sake of preformance, you can drop the sqrt and instead square the radius you're comparing the distance to.
rrh 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 08:25 AM.

///
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.