100 likes | 157 Views
Arrays in Flash. What is an Array: Array is a list of cells, each cell in the list has a number to identify it (index or position). Each cell can be used to hold information such as text, or a number, or an image.
E N D
Arrays in Flash • What is an Array: • Array is a list of cells, each cell in the list has a number to identify it (index or position). • Each cell can be used to hold information such as text, or a number, or an image. • Note: An Array size or length are the same thing. It refers to the number of cells that store information in the Array.
Arrays in Flash • To declare an array variable inside flash, use the following general form: var array_name:Array = new Array(); Ex) var x:Array = new Array(); • This statement will generate an empty array. • To insert a text element to this array: X[0] = “some text”; //Here the text value (“some text”) will be added to the first element //of the array. • To insert a new numeric element (at position 1): X[1] = 4235; // here the value (4235) will be added to the second element of the //array.
Arrays in Flash • Adding new elements to this array: • x[2] = “ab”; • X[3] = 1000.6; • X[4] = “asd”; • You can notice that arrays created in flash can hold values with different data types. x 0 1 3 4 2 positions 4235 “some text” “ab” 1000.6 “asd” elements
Arrays in Flash • To get a value from the Array use: • myArrayName[x]; //Where x is index of the value from the Array you want to fetch. Ex) to print the value stored at index (2) of the array defined in the previous example: trace(x[2]); • Counting the Length of an Array: • To find the length of an array in flash use: array_name.length;
Arrays in Flash • ex) the statement: trace(x.length); will print (5) because array (x) has 5 elements. Exercise) • Try (push()) function to add elements to an array • Try (reverse()) function to order array elements in reverse order
Array Application • Ex) This example shows how to control mouse cursor. The movements of mouse cursor will be captured and stored in an array. Then, these movements will be read and re-played again. • Create a new flash file. • Write stop(); statement in the action panel of the first frame. • Insert a button symbol onto the stage and write “Start capturing” as a caption. • Add the following actionscript to the action panel of the button. Select the button then click F9 the write:
Array Application on(release) { gotoAndStop(5); } • Insert a key frame at frame# 5. • Write the following action script in the action panel of frame#5: var xa:Array=new Array(); var ya:Array= new Array(); onEnterFrame = function() { xa.push(_xmouse); ya.push(_ymouse); } _xmouse : return the x-coordinate of the current location of the mouse cursor on stage This process will continue repeatedly Both valuse will be added to both arrays _ymouse : return the y-coordinate of the current location of the mouse cursor on stage
Array Application • Add a button onto the stage at frame#5 with “Stop capturing” as a caption. • In the action panel of that button, put the following actionsript: on(release) { delete onEnterFrame; gotoAndStop(10); } This statement is used to stop the running onEnterFrame function Then, the movie transfers and stops at frame 10
Array Application • Insert a keyframe into frame#10 • Insert a movie clip symbol onto the stage at this frame and give it (m) as an instance name. • In the action panel of this frame, write the following actionscript code:
var i:Number=0; onEnterFrame=function() { if(i<xa.length) { m._x = xa[i]; m._y = ya[i]; i=i+1; } else { delete onEnterFrame; play(); } } This onEnterFrame repeats the process of reading x and y- coordinate values from the arrays (xa,ya) and giving them to the x and y coordinates of the (m) symbol. This will result in moving the (m) symbol