by
kirupa | 23 October 2005In the
previous page, I started
explaining the code. Let's pick up where we left of with the drawpath code.
- numTargets =
initial_targets.length;
- numPaths =
initial_paths.length;
- final_paths =
[];
- final_targets =
[];
I first declare and initialize four variables. The first two variables store
a number representing the number of items in our initial_targets and
initial_paths arrays. The next two variables refer to arrays, and I
initialize them with brackets representing an empty set.
- for (m=0;
m<numTargets;
m++)
{
- cirA =
initial_targets[m];
- for (j=0;
j<numPaths;
j++)
{
- cirB =
initial_paths[j];
- if (eval(cirA).hitTest(eval(cirB)))
{
- if (contains(final_targets,
cirA)
!= 1)
{
- final_targets.push(cirA);
- }
- if (contains(final_paths,
cirB)
!= 1)
{
- final_paths.push(cirB);
- }
- }
- }
- }
This large section of code should look familiar to you if you have already
taken a look at
Page 2 of the Multiple Object Collision Detection
tutorial. There is one major difference between our code and the code used in
that other tutorial, though, and I will be covering that difference in detail
here. I strongly suggest you look and understand how the code works in the above
underlined link. It will better help you to appreciate the subtle change in the
code for this tutorial.
In our multiple object collision code, the initial value condition of our
inner for loop was j = m + 1 where m was the index variable for the outer
for loop. That would work great if you are checking for collisions among
a handful of the same objects. In our case, we are not checking for collisions
among our target movie clips. Instead, we check collisions among two different
sets of objects. We are checking for a collision among our target movie clips
and among our circle path movie clips. The names of those movie clips are are stored in our
initial_targets and initial_paths arrays.
Our earlier code would have worked only if we are checking for a collision
among either only movie clips referenced in our initial_targets array or our
initial_paths array. Since we are combining our collisions to work among both
sets of movie clips stored in both arrays, our shortcut method will not work. We
have to scan through the full range of movie clips for both arrays.
- if (contains(final_targets,
cirA)
!= 1)
{
- final_targets.push(cirA);
- }
I created a separate function called contains that very closely resembles the
indexOf function. indexOf searches through an array containing string values. If
a specified search term is found within the array, the indexOf function returns
the position the string appears in.
In my function, you are not limited to only searching for strings. You can
search for numbers, objects, etc. The variable cirA and cirB store a movie clip
name from our initial_targets and initial_paths arrays, so I search our
final_targets array to see if cirA is one of the values it contains. If that
particular value is not contained in our array, I add the movie clip reference
from cirA to our final_targets array by using the push command.
The reason I do that is to avoid duplicate references to the same movie clip
stored in our final_targets array. In the future if the same value is found, the
contains function will return a 1, thus making sure that the particular value of
cirA will not be added again to our final_targets array.
- if (contains(final_paths,
cirB)
!= 1)
{
- final_paths.push(cirB);
- }
This is the same as the above section of code I explained. The only
difference is that I check for a collision with objects in our final_paths array
and add those collided objects to our array while, at the same time, ignoring
duplicates.
- moveAway();
Finally, I call a function called moveAway. It is the last thing that our
collisionDetect function executes. The moveAway function moves any squares that
collide with your path circles. This function ensures that your main circle
movie clip has a clear target out.
Onwards to the next page
where I discuss the moveAway function. You are almost done!
 |
page 4 of
6 |
 |
|