Saturday, July 19, 2008

Basic Variables and Arrays

Two of the most useful things in flash are variables and arrays. Variables are used to store a number, letter, string of words, or just about anything. Arrays are used, like variables, to store things, but they can be used in ways variables cannot. To create a variable simply select the first frame in the timeline and go to the actions panel. Then type:

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.

Saturday, July 12, 2008

Learning the Basics

This tutorial will cover a few of the most basic functions of actionscript. One of the most used set of functions, and most commonly forgotten to mention in tutorials due to everyone assuming everyone else knows them is the gotoAnds. There are two main functions in this set: gotoAndStop() and gotoAndPlay(). Each of these is used to seek and and either stop at or play from a frame in the timeline.
For example, if you wanted a movie to goto the 5th frame and stop there from frame 1 you'd put _root.gotoAndStop(5) in the actions for the first frame (the actions panel can be accessed by pressing f9 or clicking on the actions tab near the bottom of the screen right above the properties tab, make sure it says actions -- frame rather than actions -- button or some such).
Likewise, you can also use stop() and play() by themselves on frames. For example if you had an animation that played for 200 frames, and you didn't want it to loop, you could simply put a stop() action on the 200th frame. Another sometimes useful set of functions is the nextFrame() and prevFrame(). These go one frame ahead or forward, respectively.
Now, if you want to use all these actions, here's a good way. You can make a play/pause/forward/back menu type thing for an animation. To do so simply add a new layer to your timeline that has blank non-keyframes until the end of your movie, or where you want there not to be buttons. Then draw a play, pause, forward, and back buttons. To convert them into buttons just select one of them and press f8. This should pop up a convert to symbol window. Be sure that "button" is selected. Then apply these actions to each button, respectively:

play button:

on(release){
_root.play()
}



pause button:

on(release){
_root.stop()
}



forward button:

on(release){
_root.nextFrame()
}



back button:

on(release){
_root.prevFrame()
}



In each case the _root. element means that the root, or base timeline, will follow that action. The on(release){ part means that what's in the brackets is what will happen when the button is released.
Here's an crappily sketched example.swf: