Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Getting started with ActionScript > Working with objects > Creating object instances | |||
Of course, before you can use an object in ActionScript, the object has to exist in the first place. One part of creating an object is declaring a variable; however, declaring a variable only creates an empty place in the computer's memory. You must assign an actual value to the variable--that is, create an object and store it in the variable--before you attempt to use or manipulate it. The process of creating an object is known as instantiating the object--in other words, creating an instance of a particular class.
One simple way to create an object instance doesn't involve ActionScript at all. In Flash, when you place a movie clip symbol, button symbol, or text field on the Stage, and you assign it an instance name in the Property inspector, Flash automatically declares a variable with that instance name, creates an object instance, and stores that object in the variable. Likewise, in Adobe Flex Builder when you create a component in Macromedia® MXML™ from Adobe (either by coding an MXML tag or by placing the component on the editor in Design mode) and assign an ID to that component (in the MXML markup or in the Flex Properties view), that ID becomes the name of an ActionScript variable, and an instance of the component is created and stored in the variable.
However, you won't always want to create an object visually. There are also several ways you can create object instances using only ActionScript. First, with several ActionScript data types, you can create an instance using a literal expression--a value written directly into the ActionScript code. Here are some examples:
var someNumber:Number = 17.239; var someNegativeInteger:int = -53; var someUint:uint = 22;
var firstName:String = "George"; var soliloquy:String = "To be or not to be, that is the question...";
true or false):var niceWeather:Boolean = true; var playingOutside:Boolean = false;
var employee:XML = <employee>
<firstName>Harold</firstName>
<lastName>Webster</lastName>
</employee>;
ActionScript also defines literal expressions for the Array, RegExp, Object, and Function data types. For details on these classes, see Working with arrays, Using regular expressions, and Object data type.
For any other data type, to create an object instance you use the new operator with the class name, like this:
var raceCar:MovieClip = new MovieClip(); var birthday:Date = new Date(2006, 7, 9);
Creating an object using the new operator is often referred to as "calling the class's constructor." A constructor is a special method that is called as part of the process of creating an instance of a class. Notice that when you create an instance in this way, you put parentheses after the class name, and sometimes you specify parameter values--two things that you also do when calling a method.
|
NOTE |
|
Even for those data types that let you create instances using a literal expression, you can still use the var someNumber:Number = 6.33;
var someNumber:Number = new Number(6.33); |
It's important to be familiar with the new ClassName() way of creating objects. If you need to create an instance of any ActionScript data type that doesn't have a visual representation (and hence can't be created by placing an item on the Flash Stage or the Design mode of Flex Builder's MXML editor), you can only do so by creating the object directly in ActionScript using the new operator.
In Flash specifically, the new operator can also be used to create an instance of a movie clip symbol that is defined in the Library but isn't placed on the Stage. For more about this, see Creating MovieClip objects with ActionScript.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000024.html