View Full Version : [AS2] Registering hittests on multiple instances
JapanMan
September 10th, 2008, 03:15 PM
alright, new question (I know, didn't take long at all):
what the best way to make it register hits on multiple enemies (ie. duplicates of "blob")? Should I declare what blob (and the enemies I'll make later) are with a var-statement, so that it doesn't refer to the instance name? Because if I copy the blob, for instance, it only registers hits on the original.
47919
(see related: Using Trace (http://www.kirupa.com/forum/showthread.php?t=308262))
Smee
September 10th, 2008, 03:32 PM
I think you'll want to use an array for that. Create one called something like 'blobs' that references all of your blobs, and then loop through it every frame checking for collision.
So:
blobs = [blob1, blob2];
onEnterFrame = function()
{
for(i = 0; i < blobs.length; i ++)
if(blobs[i].hitTest(_root.player))
trace(blobs[i]._name + " has been hit");
}
JapanMan
September 10th, 2008, 03:37 PM
so the instance names of the various blobs would be blob1, blob2, etc. right?
Smee
September 10th, 2008, 03:39 PM
Yes, but the names are arbitrary, as long as the ones given in the array match the names given to the blobs on stage.
Later on, you may want to create blobs dynamically using attachMovie, so in that case you would simply use the 'Array.push(instanceName)' method to add them to the array.
SparK_BR
September 10th, 2008, 06:35 PM
no matching is required since blobs[0] is equal to blob1
Smee
September 10th, 2008, 07:07 PM
no matching is required since blobs[0] is equal to blob1
If the instance names given in the array don't match to the ones given to the blobs on stage, then there is no reference. If you have an array '[circle1, circle2, circle3]' and name the objects on stage 'blob1', 'blob2', and 'blob3', then the array does not reference the blobs.
After that though, you're right that in using the array, you don't need to know the names of the blobs. That's the point of the array.
JapanMan
September 11th, 2008, 03:36 PM
that works great for the blobs, but if i use the same code for other objects, say a wall or a spinning thing, it doesn't work. What do I need to do to allow multiple arrays?
Smee
September 11th, 2008, 03:45 PM
If you want them all to react in the same way, you can just have then all in the same array:
collisionObjects = [blob1, blob2, wall1, wall2, spinner1, spinner2];
onEnterFrame = function()
{
for(i = 0; i < collisionObjects.length; i ++)
if(collisionObjects[i].hitTest(_root.player))
trace(collisionObjects[i]._name + " has been hit");
}
That would be for a game where you don't want to touch anything or it's game over.
If you want to have different reactions, that becomes more complicated. There are many different kinds of collision detection for walls, so would have to choose one.
JapanMan
September 11th, 2008, 03:57 PM
That is the idea - don't touch anything. Then I'll just have one "goal" object that you need to reach.
Thanks, big help.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.