Code Hints and Naming Conventions


Even though commenting is very helpful, and is good coding practice, sometimes you take a variable for granted and think that anyone will be able to tell what that variable is with or without a comment. And that may be true if you practice good naming conventions.

When naming variables, instances, functions, methods, and events, it is important to use names that can be easily understood. For instance, if you have a variable that represents the beginning horizontal position of an object on the stage, which of the following would you be more apt to recognize as what it is?

 var startX = myMovie_mc._x;          or var s1 = myMovie_mc._x; 

Obviously the first variable is named something appropriate to what it is holding, whereas the second variable, even though it is holding the same information, does not give you a clue about what it holds. As its creator, you will remember (at least for a short time) what the variable is, but the next person to look at your code will have no idea what the variable is for.

Some rules to follow when naming instances and variables:

  • They must start with either a letter or an underscore (_).

  • They may not have any spaces within them.

  • They cannot be one of the reserved keywords such as var or this.

  • Although this is not a rule, but more of a warning, you should also not give variables the same name as object classes such as Color or Date, which can cause errors in your coding in certain situations.

Also, when creating instances of objects, there are special suffixes that can be added to the ends of the names to make the code hints pop up. Code hints are used to assist developers when working with objects. When the Actions panel knows what object type you are working with, it can give you a list of all the available methods, properties, and events of that object so that you don't have to remember them all.

To see an example of code hints, follow these steps:

1.

Create a new Flash document.

2.

Open the Actions panel in the first frame of the main timeline and create a string like this:

 var myString_str = "test"; 

3.

Now, after that line of code, begin typing the string's name (myString_str) and then type a period after the last character of the name. The code hints will pop up.

If the code hints do not appear, make sure they are turned on by going to the Show Code Hint button in the Actions panel.

What controls these code hints?

ActionsPanel.xml

The code hints for objects are controlled in an XML file at C:\Program Files\ Macromedia\Flash 8\en\First Run\ \ActionsPanel\ActionScript_1_2\ ActionsPanel.xml for Windows users and Applications\Macromedia\Flash 8\en\ First Run\ActionsPanel\ActionScript_1_2\ActionsPanel.xml for Mac users. You can view all of the suffixes as well as other hinting tools at the bottom of those files. Or you can use this list to see the available suffixes:

  • MovieClip_mc

  • Array_array

  • String_str

  • Button_btn

  • Text Field_txt

  • Text Format_fmt

  • Date_date

  • Sound_sound

  • XML_xml

  • XML Node_xmlnode

  • XML Socket_xmlsocket

  • Color_color

  • Context Menu_cm

  • Context Menu Item_cmi

  • Print Job_pj

  • Movie Clip Loader_mcl

  • Error_err

  • Camera_cam

  • LoadVars_lv

  • LocalConnection_lc

  • Microphone_mic

  • NetConnection_nc

  • NetStream_ns

  • Shared Object_so

  • Video_video

Of course, using suffixes is not the only way to trigger code hints. You can also declare the data type in comments.

Triggering Code Hints with Comments

You do not have to use the suffixes with object names to get code hints to work; you can also declare the object's data type in a commented line. After that, the object's name will pop open the code hints for that object.

To declare a data type of an object on a commented line, you first put the two forward slash marks (//), followed by the object class name (i.e., Array, String, Color), then a space followed by the object's instance name, and finally a semicolon to end the line.

For example, place this code in the first frame of the main timeline in the Actions panel:

 //Color myColor; 

Now whenever myColor is used, it will pop open the code hints for the Color object.

There is one final way to trigger the code hints, and we have already used it: strict data typing.

Strict Data Typing to Trigger Code Hints

You can trigger code hints by declaring the object's data type in a commented line because you are declaring the object's data type. Strict data typing does the same thing without the commented line.

Place this line of code in the first frame of the main timeline in the Actions panel:

 var myString:String = "test"; 

Now when you use the myString string, when you place a period after its name in the Actions panel, the code hints for the String object will appear.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net