myvar = 10
trace (myvar)
What's this trace thing you ask? Export the movie and you'll see. A little box pops up displaying 10 in it! The trace function traces whatever is inside the ()s. In this case it was myvar, which was equal to ten. Now try this:
myvar = 10
trace("myvar")
What's this!? It traces myvar rather than 10!? This is because it traces the string "myvar" rather than the variable. Anything inside either 's or "s will be traced as exactly what it says in the actions panel. Now, here's another thing that can get confusing:
trace("bob said "I am going for a walk" and walked off")
The above will not work, due to the script registering the second quote as the closing quote. However, to fix this we can simply exchange on type of quote for another.
trace('bob said "I am going on a walk" and walked off')
Now it should work, because now everything is inside 's, and even though there are two "s, it doesn't matter because the program won't register those as the closing quotes.
Now for arrays. First off, to declare an array do this:
myarray = new Array()
That will create an array named myarray. Every array is made up of elements. To access an element type this:
myarray = new Array()
myarray[0] = 2
trace(myarray)
That will trace 2 in the output, hopefully. Notice that the number for an array element starts at 0 rather than 1. In case you hadn't picked up on this, to change an array's element type the arrays name followed by the element number in []s. You can also create an array with a certain length, or a certain element or elements.
myarray = new Array(20)
//will create an array with a length of 20
trace(myarray)
//this will trace undefined 20 times in the output, because you haven't defined any of the arrays elements.
myarray2 = new Array(1,2,3,"fifteen thousand!!")
//this will create an array with a length of 4 and the 4 elements being 1,2,3 and fifteen thousand!! respectively
Another nifty trick with arrays in that an element in an array can be an array in itself! Here's an example:
myarray = new Array()
myarray[0] = new Array(1,2,10)
trace(myarray)
That should trace 1,2,10 in the output. Pretty cool huh?
Applications of arrays and variables:
Variables: Just about everything from velocity to whether or not two objects are touching
Arrays: Very useful for tilebased game engines, as well as keeping track of large sets of numbers e.g, points around the edge of a circle.