Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-03-2010, 12:50 AM   #1
Registered User
Join Date: May 2010
Posts: 2
Default [AS2] Multiple instances hitTested

In my game, multiple movieclips have been added with the duplicate movie clip function.
Here is the code for that :

ActionScript Code:
i=i+1; duplicateMovieClip     (_root.projectile, "projectile" +i, i);     _root["projectile" + i]._x = _root.Char._x;     _root["projectile" + i]._y = _root.Char._y-200;     }

I want to know if there is any way to see if any of those movie clips with the projectile + i instance have collided with another object. Is this possible?

Any help would be much appreciated!
wallross is offline   Reply With Quote
Old 10-03-2010, 03:56 AM   #2
I'm back, baby!
dzy2566's Avatar
Join Date: Jul 2003
Location: Palm Desert, Ca
Posts: 1,148

Yes, you just need to loop through your projectiles and check for collisions against the other object(s). Depending on how you're managing your projectiles, you might want to store them in an array. So each time you duplicate a movieClip to make a new projectile, add it to an array. For example:

ActionScript Code:
// array to hold references to your projectiles var projectiles = new Array(); // create 10 projectiles for(i=0;i<10;i++){     duplicateMovieClip(_root.projectile, "projectile" +i, i);     _root["projectile" + i]._x = _root.Char._x;     _root["projectile" + i]._y = _root.Char._y-200;     projectiles.push(_root["projectile" + i]); } // call this function (probably in an enterFrame) to check for collisions // this assumes you are only checking for collisions again 1 object "ball" function checkForCollisions():Boolean{   for(i=0;i<projectiles.length;i++){     if(ball.hitTest(projectiles[i]){       return true;     }   } } // if you have multiple objects to check against, you can use nested loops var balls = new Array(); function checkForMultipleCollisions():Boolean{   for(i=0;i<projectiles.length;i++){     for(j=0;j<balls.length;j++){       if(balls[j].hitTest(projectiles[i]){         return true;       }     }   } }

You might also want to look into using attachMovie instead of duplicateMovieClip. AttachMovie allows you to give library items linkage identifiers that can be used to pull items out of the library at runtime and attach them to the stage or another movieClip. This way you don't have to place items on the stage at author-time to duplicate at runtime. For example:

ActionScript Code:
// assume movieClip in library has linkageID of "projectile" function createProjectile(clipToAttachProjectileTo,x,y):MovieClip{   var depth = clipToAttachProjectileTo.getNextHighestDepth();   var tempVar = clipToAttachProjectileTo("projectile","p"+depth,depth);   tempVar._x = x;   tempVar._y = y;   projectiles.push(tempVar);   return tempVar; }
Bacon and Games
A site dedicated to game design and programming (and bacon!!!)
dzy2566 is offline   Reply With Quote

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 06:50 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.