For this tutorial I will be making a really basic game where balls fall from the ‘roof’ and you must stop them with a paddle, I have excluded any scoring/end game flags to keep this as pure as possible.
The parentBall
Firstly draw a circle on the main timeline, select it, and create a movieClip out of it (F8).
Now select the circle, and in the ‘Instance Toolbar’ set its name to ‘ballParent’.
Then right click on the circle and select: Actions.
Now the first bit of actionScript we need is following.
All this does is the following:
If the balls name is ‘ballParent’ (which it is) we will create a new array on the root of the movie called ‘array’. This will be the array that holds the names of the duplicated balls.
We also create a variable called ‘arraySize’ on the root of the movie. This will be used to check the size of the array.
Finally we create two more variables which will be used later.
If the balls name is not ballParent, we defined some other variables which I will describe later.
Next add the following code:
This is the ‘heart’ of the system.
First I will explain at the code for the ballParent.
Every frame ticks is increased by one, if ticks is greater than 25, we then duplicate the ballParent. This will give us our duplicatedBalls.
Every time a ball is duplicated we give it its array-name. If the ball is ‘ball0’ then its array-name will be ‘0’ (I am using integers to keep the memory usage lower, however we could make its name its actual name using the _name expression). Then we add this array-name to the array. I will describe what is happening.
For example, if the array is of size ‘0’, then we need to give the first element the array-name of our duplicated ball. If the array is of size ‘5’ then the 6th element must be given the name. Because an array’s first element is in fact 0, we can just say:
ARRAYNAME[ARRAYSIZE] = NAME
We then increase the array size, and increment i.