Macromedia Flash Professional 8 Unleashed
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:
Next, on the main stage, create four layers with the following labels:
The movie will consist of three keyframes. In the Labels layer, label the frames like this:
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. |
Категории