If we look at the script for if our ball’s name is not ‘ballParent’ we will see the following.
As you may have seen, the original ball’s name is ballParent, but when we duplicate the clip, we give it another name, that means that it will NOT execute the definition of the array again etc.
On load, we define its _x value as a random position on the screen, give it a falling speed (random of course), and a variable called ‘move’ which is set to 1.
This move variable is quite important, when it is set to 1, the clip will go about its job normally, however when it is set to 0, the clip will remove itself from the scene.
If move is equal to 1, we have 4 or so simple lines of code to move it down by its speed every frame, then if it is lower than the screen to set ‘move’ to 0 (to remove itself)
Now if ‘move’ is set to 0, we must remove the clip’s name from the array, this is the most difficult part of this tutorial to understand so bear with me, again here is the code:
What this code does is as follows:
Step through the array one by one, and compare that value to the balls own ‘name’ (given on duplication).
If the name in the array is equal to its own name, we then continue through the array but put the next element onto the current element, in essence, once we step the array back 1, from the point where we found our ‘name’, we are deleting it, as it is no longer in the array. We then remove the final element (as it is just a copy of the last element)
Here is a little picture of what we do to help you understand.
Array, (arraySize == 5) :
|0|1|2|3|4|
If we remove the ‘2’ we step all the other elements from 3 on back one.
Array, (arraySize == 5) :
|0|1|3|4|4|
Then we delete the last element.
Array, (arraySize == 4) :
|0|1|3|4|
We are left with the array we want.