Accessibility

ActionScript cookbook beta

Create a typed variable

Problem Summary

You need to create a variable to store a specific type of value.

Solution Summary

Use the var keyword and specify the variable type.

Explanation

ActionScript 2

var s:String = "I am a String";
var i:Number = 5;

ActionScript 3

var s:String = "I am a String";
var i:Number = 5;

Note: Once you type the variable, you can only store data of that type in the variable. For example, the following would throw an error:

var s:String = "I am a String";
var i:Number = 5;

s = i;

because you are trying to store a Number in a variable which you specified was a String.

The advantage of typing variables is that, in general, it makes your code a little easier to read, the player can run your code faster, and the compiler can find some errors when you compile your content.

All of the examples in this cookbook use typed variables.