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-01-2012, 05:56 PM   #1
babblebit
Registered User
 
Join Date: Jul 2012
Posts: 8
Default [AS3] Hittest any movieclip?

Is it possible to perform a hittest on every movieclip with a certain property?

For example, could you get a game character controlled by the arrow keys to stop whenever it came into contact with any movieclip 100 pixels wide?
babblebit is offline   Reply With Quote
Old 08-01-2012, 06:58 PM   #2
zadvorsky
Registered User
 
Join Date: Aug 2012
Posts: 2
Default

Well, you can perform a hit test on every object, and only call the hero-stopping-method if the width of an object is 100 pixels.

Or you can loop over all objects, push those with a width of 100 into a separate array, and perform a hit test with each object in that array.
zadvorsky is offline   Reply With Quote
Old 08-02-2012, 03:39 AM   #3
tango88
FlashByNight.com
 
tango88's Avatar
 
Join Date: Aug 2006
Posts: 284
Default

You should always carefully track all objects onscreen in a game.

For example, I track all enemies in a game using enemyArray. This is an array of objects.

I use 'hero' as the name of the player's object:

Code:
for(i=0;i<enemyArray.length;i++){
   if(enemyArray[i].width==100 && hero.hitTestObject(enemyArray[i]){
         //insert relevant action here
   }
}
__________________
Games and more:
Flashbynight.com | Tutes | Twitter
tango88 is offline   Reply With Quote
Old 08-02-2012, 06:39 AM   #4
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 1,898
Default

You can use the for-each-in loop with Arrays.
It's a bit more compact and orderly, and it might be slightly faster.
Also, since all of the enemies are identical types, you can use a Vector instead of an Array in Flash 10 or higher. It processes much faster.
ActionScript Code:
for each (var e in enemyArray){    if(e.width==100 && hero.hitTestObject(e){          //insert relevant action here    } }
[afz]snickelfitz 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 05:20 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, 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.