Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums check if movieclip is within a circular radius of another movieclip
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 05-15-2012, 09:34 AM #1 jpoopdog Member   Join Date: Feb 2012 Posts: 52 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?
 05-16-2012, 09:17 AM #2 tadster tadster     Join Date: Feb 2009 Location: Texas Posts: 2,111 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.
 05-17-2012, 07:34 AM #3 jpoopdog Member   Join Date: Feb 2012 Posts: 52 looks a bit complex. ill see if i can add it to the game, thanks
 05-17-2012, 04:24 PM #4 rrh throw a trace() in there   Join Date: Dec 2006 Posts: 1,982 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.

 Thread Tools Display Modes Rate This Thread Linear 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 01:43 PM.

///