Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-31-2012, 01:34 PM   #1
ftuzz
Registered User
 
Join Date: May 2012
Posts: 6
Question can a round hit box be made?

So i'm making my first as3 game and its just a simple avoider style game. I've got most of the basics working but the objects that are being avoided are circles and since im using hitTestObject i get a square hit box . Is it possible to create a circular hit box?

Heres my code

ActionScript Code:
package {     import flash.display.*;     import flash.events.*;     import flash.text.*;     import flash.utils.Timer;     import flash.utils.getDefinitionByName;         public class mattGame extends MovieClip {         var ship:Ship;         var gameTimer:Timer;         var army:Array;         var score:int = 0;         const speed:Number = 7.0;                 public function mattGame() {                 army = new Array();             var newEnemy = new Enemy( 250, -15 );             army.push( newEnemy );             addChild( newEnemy );                         ship = new Ship();             addChild(ship);                         gameTimer = new Timer( 25 );             gameTimer.addEventListener( TimerEvent.TIMER, onTick );             gameTimer.start();         }         public function onTick( timerEvent:TimerEvent ):void {             if ( Math.random() < 0.1 ){                 var randomX:Number = Math.random() * 500;                 var newEnemy:Enemy = new Enemy( randomX, -15 );                 army.push( newEnemy );                 addChild( newEnemy );             }             ship.x = mouseX;             ship.y = mouseY;                         for each ( var enemy:Enemy in army ){                 enemy.moveDownABit();                 if ( ship.hitTestObject( enemy ) ){                     gameTimer.stop();                 }             }         }     } }

end the enemy class to

ActionScript Code:
package {     import flash.display.MovieClip;     public class Enemy extends MovieClip     {         public function Enemy( startX:Number, startY:Number )         {             x = startX;             y = startY;         }                 public function moveDownABit():void         {             y = y + 7;         }     } }
ftuzz is offline   Reply With Quote
Old 05-31-2012, 03:46 PM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

No you cannot do a circular hitbox but circle are the easiest object to detect a collision from. Simply calculate the distance from the center of the circle to whatever is colliding with it and if that distance is smaller than the circle radius you got a hit.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 06-01-2012, 12:31 AM   #3
dialectric
Senior Member
 
dialectric's Avatar
 
Join Date: May 2006
Location: USA
Posts: 614
Default

As ASWC said, there is no built in feature to do this. You could calculate it on your own, or employ one of a number of pixel-perfect hit tests that others have written such as:

http://troygilbert.com/2007/06/pixel...actionscript3/

- dialectric
dialectric is offline   Reply With Quote
Old 06-01-2012, 03:44 PM   #4
ftuzz
Registered User
 
Join Date: May 2012
Posts: 6
Default

@ dialectric thanks for that, but it’s a little too complicated for my feeble mind to comprehend .

@ ASWC but my character isn’t a circle so how do i take into account the different dimensions?
ftuzz is offline   Reply With Quote
Old 06-04-2012, 03:20 PM   #5
ftuzz
Registered User
 
Join Date: May 2012
Posts: 6
Default

actually with much thinking i go it working so big thanks dialectric
ftuzz 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 10:19 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.