Variable Hints [Coding Efficiency]
by thoriphes aka manny dimatulac - 25 January 2004
This short article is on variable naming in the
new Actionscript 2.0 (and 1.0, I guess).
I like to bring some light to a little known fact about naming variables in 1.0:
You know how some variables will bring up a list of available functions and
properties when you type a dot at the end of it?
- //syntactically wrong, but for the sake of example
- my_mc. // will bring
up a list of functions available to that variable
- // whereas...
- myMC. // won't
What triggered the list of available functions
for the first command was actually the suffix "_mc". The variable types in AS
x.0 (for both versions 1 and 2) each has its own suffix to enable what is called
Automatic Code Hinting.
If you're anything like me, I don't like typing
out some of Flash's really long function names like:
So this is where suffix-ing your variables comes
Like I mentioned, each data type (Array, Color, MovieClip, etc) has its own
little tag along suffix that will enable the code hinting. They are as follows:
(available for both AS 1.0 and 2.0, incomplete)
- Array: _array
- Button: _btn
- Color: _color
- Date: _date
- MovieClip: _mc
- Sound: _sound
- String: _str
- TextField: _txt
- TextFormat: _fmt
- XML: _xml
- XMLSocket: _xmlsocket
As you can see, the suffix for any given type is pretty obvious.
So how do we apply this? Well, given that list, you can now add those suffixes
to your variable names (like my_array, preload_sound, etc) and a menu with every
available function for that type will appear. So now you can see why, in the
code above, my_mc triggers a menu and myMC does not. Use these suffixes to your
advantage, whether for speed or reliability, it is your friend.
Now on to AS 2.0...
2.0 also offers those suffixes that were listed above as well as some new ones:
(AS 2.0 only):
- Camera: _cam
- ContextMenu: _cm (my next tutorial will be on this)
- ContextMenuItem: _cmi
- Error: _err
- LoadVars: _lv
- LocalConnection: _lc
- Microphone: _mic
- PrintJob: _pj
- NetConnection: _nc
- NetStream: _ns
- SharedObject: _so
- Video: _video
- XMLNode: _xmlnode
Now there is another way to display code hints without the use of suffixes. It
is implemented in AS2.0's new style of variable declaration:
- //Now instead of using _str to display the menu
- //You can have the same menu appear but name the
variable whatever you like:
- var myString:String;
- myString. // pops-up
menu with string functions
Now if you haven't seen this syntax for variable
declaration, don't fear. It's quite simple, really:
var variable_name:Data Type
It's up to you whether you want to use var or not, it doesn't make a difference.
But if you do, a menu of available data types appears when you type that colon
used in the syntax (how nice of them!).
Well that's all I have for now. If you have any questions/corrections to this
article, let me know.