Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Categories Implementing a hitTest array
Author: Chris Rhodes | Website: http://www.snakenetworks.com |

 
Page 2
«prev 1 2 3 4 5 next»

Implementation

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.
onClipEvent (load) {
 if (_name == "ballParent") {
 _visible = 0;
 _root.array = new Array();
 _root.arraySize = 0;
 ticks = 0;
 i = 0;
 } else {
 _x = _width/2+random(550-_width/2);
 speed = 2 + random(4);
 move = 1;
 }
}
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:
onClipEvent (enterFrame) {
 if (_name == "ballParent") {
 ticks++;
 if (ticks> 25) {
 this.duplicateMovieClip("ball"+i, i+10);
 _parent["ball"+i].name = i;
 _root.array[_root.arraysize] = i;
 _root.arraysize++;
 i++;
 ticks = 0;
 }
 } else {
 if (move == 1) {
 _y += speed;
 if(_y> 410){
 move = 0;
 }
 } else {
 for(i = 0;i <_root.arraysize;i++){
 if(_root.array[i] == name){
 //trace(_root.array);
 for(j = i;j <_root.arraysize-1;j++){
 _root.array[j] = _root.array[j+1];
 }
 delete _root.array[_root.arraysize];
 _root.arraysize--;
 i = _root.arraysize;
 }
 }
 removeMovieClip ("");
 }
 }
}
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.
_root.array[_root.arraysize] = i;
_root.arraysize++;
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.

«prev 1 2 3 4 5 next»

» Level Advanced

Added: : 2002-01-25
Rating: 8.48 Votes: 35
Hits: 4131
» Author
Chris 'PrED' Rhodes
» Download
Download the files used in this tutorial.
Download (5 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments
 
   
 


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers