Wednesday, April 9, 2008

Variables

Today I figured out a bit more about variables. It seems that in Flash, everything is 'placed' within layers. The layers are determined by movie clips within movie clips.. etc. For instance, to access a clip (foot1) inside of another clip (player) the programmer would have to write _root.player.foot1 This approach goes for variables as well. If a variable (speed) is declared in the foot1 mc, then to access it the programmer must write _root.player.foot.speed ... seems simple enough, right? Well coming from the background of C++ where there were no layers, I assumed that the variables would all be accessible from all code anywhere. Wrong... but the good news is, making variables is very easy. It's as simple as:

var variablename:variabletype = some_initial_value;

For instance...

var money:Number = 0;
//code placed at the very top level of the movie (not in any mc's)

To access this from any mc I would write:

_root.money

For instance, if I wanted to add 50 to the value of money when the player gets 50 moneys, I would put this code:

_root.money += 50;

Simple! This is the great part about AS, once I figure out what the syntax is, it's very fast. I could write out the code to make a pulldown menu function with only a few lines.

Something else that I found out was AS supports "include" files. For instance, I have some chunk of code that makes the mc move with the arrow keys. I could create an actionscript file (.xml if I am remembering correctly) called "arrowkeycontrol.xml" which could be saved in the same directory as the .fla file. I could then type the code #include "arrowkeycontrol.xml" into any mc in that program and it would be controlled by the arrow keys. I could very well have the syntax wrong here, but finding that out is very easy with the actionscript library! I've found it to be the most useful help menu I've encountered!

No comments: