Logo link to Tomontheweb.ca
Flash Server

The Basics of ActionScript

Strict data typing and code hints

One of the defining features of ActionScript 2.0 is the introduction of strict data typing. All this means is that each variable you create is tied to a class. For example, var groceries = “Lettuce”; has a problem. The variable could also use a number instead of a word. This leaves you wide open to potential errors. Strict data typing ensures the only thing that can be associated with the groceries variable is a word. The other advantage to using strict data typing is that it lets you use code hinting in the Actions panel

Using strict data typing

Strict data typing tells Flash what data type is associated with a variable based on the class associated with the data type. The type of data associated with groceries will be the word “lettuce”. Lettuce is a string and there is a String class in Flash. Associate “lettuce” with that class and all of the class’s methods and properties are available to the word “Lettuce”.

Therefore the variable would be declared in the following manner:

var groceries:String= “Lettuce”;

Because you only have to “strict type” a variable when it is created, you no longer have to worry about what value is used for the variable. If you use the wrong value, Flash will let you know when you publish the movie. Let’s look a couple of examples. Here’s and example of a mistake:

var groceries:String= “Lettuce”;
groceries = 12;

When the movie is published, or when you test it, you will get an error. You are getting the error because you assigned a number to the variable and Flash is telling you that you can’t do that. Flash is expecting a string such as in this code:

var groceries:String= “Lettuce”;
groceries = “Carrots”;

Using code hinting in the Actions Panel

Code hints are contained in a wonderful little pop down box when you enter your code. The beauty of code hints is they reduce the amount of time you spend typing and reduce the number of typos and errors that might break your code.

Open a new Flash document, select the first frame and press F9 (Option-F9…Mac) to open the Actions panel.

When the Actions panel opens click once in the Script pane and type:

var groceries:

When you press the colon key a code hint pop down menu appears that lists all of the classes that can be associated with a variable. Select “String” from the list. It will be entered into your code.

When it appears finish the variable declaration by entering:

Var groceries:String = “Lettuce”;

2) Press the Enter/Return key and enter:

groceries.

As soon as you type the period all of the methods and properties associated with a string will appear in the list. Double click one of the items and it will be added to the code.


You can turn off Code Hinting in the Flash Actionscript preferences. To turn it off, simply deselect Code Hinting when the panel opens.

Code hints are invaluable, especially when you are manually entering your code and can’t remember which property or method is associated with the variable or action. There are two ways of having the code hints appear: use strict data typing or add a suffix to a variable.
As you work with code throughout this book you will encounter both methods used with variables and instance names.

Using suffixes is a an older practice used by AS1 programmers. However, with strict typing, it is no longer necessary, and will allow a more conventional naming practice. However, for the sake of example, assume you have a movie clip named myMovie on the stage. When referring to it in your code that name means absolutely nothing without strict typing. Give it the instance name of myMovie_mc and ActionScript knows what it means. ActionScript will provide the methods and properties that pertain to a movie clip. Other suffixes include _btn (Button), _txt (Text Field) and _str (String).

There is a another method of getting Flash to display code hints. Simply write the data type and the variable within a comment:

//MovieClip myMovie;
myMovie._x = 6;

Comments are messages and descriptions that you type into the code. A typical comment might be:

// This code moves the movie clip 6 pixels to the right.

The “//” tells Flash this is a comment and to ignore it. However, if you use a comment for the data type and the variable name, as soon as you type the period after the variable name, the code hints will appear.

I am fond of telling anybody that will listen; “There are 6,000 ways to do anything in the digital world. Find what works best for you.” So which is the best way? Pick one.

...NEXT

photo Tom Green
Tom Green

Who is tom green?

Teacher, author, raconteur. Here's a run down of what I have been up to over the past few years. My Bio.

Classes

Class Project 1