February 23, 2009
ActionScript 3.0 Basics: Objects & Punctuation!
ActionScript 3.0 has roots in Object Oriented Programing, and thus a higher level of complexity and it can even posses a higher level of usability.
In short Object Oriented Programing, refers to the use of Objects to create desired results, or to take certain actions.
For example, let us pretend that we have an object, a phone in its simplest form. The phone object would have certain properties to it, such as dimensions(with, height, dept), weight, color, and so on. The phone object would also have methods, or functions which would perform desired task’s.
In the world of OOP(Object Oriented Programming), specifically ActionScript 3.0, if you wanted to create or instantiate a phone object you would do so by storing it in a variable and using the keyword new like so:
//Two variables, each containing an instance of the Phone object. //Each instance is independent of the other. var myPhone:Phone = new Phone(); var yourPhone:Phone = new Phone();
Now you have instantiated, or created an instance of a Phone, which is defined by a class named Phone.
A class is in layman’s terms, a blueprint for an object. It defines how it should behave, its methods, and what properties it contains.
Dot Syntax
So now we have our Phone objects instantiated. We are now able to access its properties (assuming their access modifier is set to public).
In order to access any Objects properties or methods within ActionScript3.0 we use dot syntax, such as:
//Create a new instance of Phone var myPhone:Phone = new Phone(); //Set the color property of myPhone object to color #336699 myPhone.color = 0x336699; //Call the dialNumer() method for the given number myPhone.dialNumber(13104441598);
In the code above we have created our Phone object and stored it in the myPhone variable, to define its color property we use the dot syntax to indicate we want to specify its color property, and the assignment operator(=) to assign it hex-decimal color 336699.
Then we decide we want to make a phone call so we decide to call its dialNumber method, which accepts one argument inside the parenthesis.
This is a short example to demonstrate how dot syntax works. The above code would probably result in compiler errors it punched into Flex Builder, or FlashIDE,unless you really did have a phone class.
Punctuation
Apart from using dots to drill down into an objects properties or methods, we have other punctuation marks that help our code be readable and specify bounds for our compiler.
‘ ’ and ” ” - Apostrophes, are used to define a collection of characters, eg ’Hello World’ or ”Hello World”
; - Semicolon, indicates end of a line, optional, but good practice.
. - Dot used to drill down into an objects properties or methods.
() - Parenthesise, are used to indicate a method or class and to accept arguments.
, - Comma, used to separate values, or arguments.
+ - Used to concatenate , or add numbers and strings. eg ’This ‘ + ‘ and ‘ + ‘ this.’ results in ‘This and this.’ or 5+5 results in 10.
- - Minus, subtraction.
for a comprehensive list of more punctuation marks and other operators and their purpose you can visit, ActionScript 3 Language and Syntax and http://livedocs.adobe.com/flex/3/langref/operators.html
Links:
ActionScript 3 Language and Syntax
Access Modifies in AS3 - Flexcomps Weblog