Applied Example

We have gone over a lot of code and different examples of how to use some of the methods of arrays. Now let's look at an applied example of arrays at work. We are going to create a mouse recorder that, after a certain length of recording, will replay the recorded positions of the mouse.

First, we must create the necessary symbols for the movie:


Create a movie symbol with an arrow graphic that's centered at the point of the arrow. Name this symbol arrowMC (see Figure 10.2).

Figure 10.2. The "arrowMC" symbol.


Create a button symbol to move from frame to frame (I used one from the common libraries under Window > Other Panels > Common Libraries > Buttons). Name this symbol button (see Figure 10.3).

Figure 10.3. The "button" symbol.

Next, on the main stage, create four layers with the following labels:

  • Actions

  • Labels

  • Arrow

  • Button

The movie will consist of three keyframes. In the Labels layer, label the frames like this:

  • frame 1 start

  • frame 10 record

  • frame 20 playRecord

In the Button layer, place a copy of the button we created and give it an instance name of "record_btn".

Now we will move to the Actions layer. In the first frame, place this code:

 stop(); //Create our arrays to hold our data var mouseX:Array = new Array(); var mouseY:Array = new Array(); //this is the event for the button record_btn.onRelease = function()        gotoAndStop("record"); } 

Next, create a second keyframe in frame 10 of the Actions layer. Place the following code within that frame:

 //Create a variable to adjust the length of the recording var time:Number = 10;  //seconds this.onEnterFrame=function(){ //Then use a loop statement to check if time is up         if(time >= Math.floor(getTimer()/1000)){ //Record the positions of the mouse and place them //in the associated arrays                 mouseX.push(_xmouse);                 mouseY.push(_ymouse);        }else { //When time is up                 gotoAndStop("playRecord");                 delete this.onEnterFrame;         } } 

Then, on the Arrow layer, place an arrow instance on the main stage with an instance name of "arrow_mc" in the third keyframe (frame 20) as well as create another keyframe in the actions layer, and place these actions in it:

 //create the incramental variable var i:Number=0; this.onEnterFrame=function(){ //as long as the you are not at the end of the array //keep playing         if(i<mouseX.length){ //Set the positions of the arrow equal to positions //held in the arrays                 arrow_mc._x = mouseX[i];                 arrow_mc._y = mouseY[i];                 i++;       }else { //When it's over, go to the beginning                 gotoAndStop("start");         } } 

That's it! Now test the movie and have some fun coming up with your own experiments using arrays. Also note that the higher the frame rate, the smoother the animation will play.

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: