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 03-11-2010, 06:21 PM   #1
johnrich85
Registered User
 
Join Date: Mar 2010
Posts: 6
Default [AS3] Collision detection

I've made a loop which will create a random number of asteroids, and a function which will create a lazer when space bar is pressed, the lazer is then added to a container.

Im attempting to get collision detection working between the two but cant get it to work. I know hit test object isnt the best way to do it, but should the code below work ?


ActionScript Code:
public function mainGame(evt:Event):void {                 // collision *************************         //for every child in the LAZER container         for (var a:Number=0; a < this.shotContainer.numChildren; a++) {             //make variable for current lazer             b = this.shotContainer.getChildAt(a) as J_lazer;                 //for every child in the ASTEROID container         for (var c:Number=0; a<this.container.numChildren; a++) {             //make variable for current asteroid             d = this.container.getChildAt(c) as J_asteroid;                         if (this.d.hitTestObject(this.b)) {                 trace('hit');             } else {                 trace('nope')         }         }         }         }
johnrich85 is offline   Reply With Quote
Old 03-11-2010, 06:21 PM   #2
johnrich85
Registered User
 
Join Date: Mar 2010
Posts: 6
Default

Enter framt event calls the function by the way
johnrich85 is offline   Reply With Quote
Old 03-11-2010, 07:03 PM   #3
TomMalufe
Salmon Eater
 
TomMalufe's Avatar
 
Join Date: Jun 2007
Location: Doylestown, PA, USA.
Posts: 740
Default

it would be faster to itterate through an array of astroids and an array of lazers rather then doing getChildAt() Otherwise that will probably work. Have you tested it yet?

I found a really cool hittest class [google ws.tink.display.HitTest;] that does a pixel perfect hittest on 2 objects while ignoring alpha. It's nice if you have a square bitmap of a spaceship flying around (or a car in my case) and you want to hittest the ship and not the bitmap.

It should be safe to use in any case because it does a simple fast hittest first and progressively gets more complex only if the tests are true. This way it's not working the CPU more then it has to.
__________________
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." -- Chinese Proverb.

Last edited by TomMalufe; 03-11-2010 at 07:05 PM.
TomMalufe is offline   Reply With Quote
Old 03-11-2010, 07:18 PM   #4
johnrich85
Registered User
 
Join Date: Mar 2010
Posts: 6
Default

Quote:
Originally Posted by TomMalufe View Post
it would be faster to itterate through an array of astroids and an array of lazers rather then doing getChildAt() Otherwise that will probably work. Have you tested it yet?

I found a really cool hittest class [google ws.tink.display.HitTest;] that does a pixel perfect hittest on 2 objects while ignoring alpha. It's nice if you have a square bitmap of a spaceship flying around (or a car in my case) and you want to hittest the ship and not the bitmap.

It should be safe to use in any case because it does a simple fast hittest first and progressively gets more complex only if the tests are true. This way it's not working the CPU more then it has to.
Cheers ill check it out. This is for a uni project, we havent got around to covering arrays yet so would prefer not to use it if possible. I have tried it, it seems like it works occasionaly, possible only for the first asteroid? Other than that it doesnt appear to work.
johnrich85 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 01:09 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.