Title: Arrays in Flash
1Arrays 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.
2Arrays in Flash
- To declare an array variable inside flash, use
the following general form - var array_nameArray new Array()
- Ex) var xArray new Array()
- This statement will generate an empty array.
- To insert a text element to this array
- X0 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)
- X1 4235
- // here the value (4235) will be added to the
second element of the //array.
3Arrays in Flash
- Adding new elements to this array
- x2 ab
- X3 1000.6
- X4 asd
- You can notice that arrays created in flash can
hold values with different data types.
x
0
1
2
3
4
positions
4235
some text
ab
1000.6
asd
elements
4Arrays in Flash
- To get a value from the Array use
- myArrayNamex
- //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(x2)
- Counting the Length of an Array
- To find the length of an array in flash use
- array_name.length
5Arrays 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
6Array 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
7Array Application
- on(release)
-
- gotoAndStop(5)
-
- Insert a key frame at frame 5.
- Write the following action script in the action
panel of frame5 - var xaArraynew Array()
- var yaArray 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
8Array Application
- Add a button onto the stage at frame5 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
9Array Application
- Insert a keyframe into frame10
- 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
10- var iNumber0
- onEnterFramefunction()
-
- if(iltxa.length)
-
- m._x xai
- m._y yai
- ii1
-
- 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