This site is now just an archive over the rise and fall of Flash. The domain flashmagazine.com is available for sale
Login | Register
Brief overview of AS3

October 17th 2005 | Jens C Brynildsen

0 comments

 

Brief overview of AS3

With the introduction of AS3, there's yet another language to learn. How radical are the changes and what are they? Rest at ease - if you know AS2, the change is not that big and there are even a few new commands you can use.

by Jens C. Brynildsen From our first looks, AS3 is not a whole new language to learn. It's the same language, but it's structured even better than AS2. You saw the beginnings of this in Flash 8. Everything now has its own class and everything is neatly subclassed. The class hierarchy is pretty complex, but it is easy to understand.

The major changes:

- There is no _global scope anymore, but you can use "namespace" to create your own namespaces in addition to the predefined public, private and internal ones.
- int / uint. New data types to represent non floating point numbers. This addition brings Flash in sync with other programming languages and will solve a few headaches when working against Java backends using AMF/Flash Remoting.
- You can no longer use the commands play() or stop() on a timeline. MovieClips are no longer in the global scope and must be accessed properly through its class flash.display.MovieClip.
- Regular expressions - quickly search for and manipulate strings.
- New and much simpler Delegation
- DOM3 Event Model - a new, but not unfamiliar way of generating and handling event messages
- Display List API - Graphics are now subdivided into new and more logically correct base classes such as Sprites and Shapes.
- There is no longer any need to assign depth numbers to objects. Depth management is now automatic (based on the Display List) and built into the Flash Player. New methods are provided for specifying and managing the z-order of objects.
- Final / protected keywords. Prevent your classes/functions from being overridden.
- New and simpler ways of accessing XML elements and attributes using EX4.
- ArgumentError Class. Makes it possible to catch errors that occur when the parameter values supplied during a function call do not match the parameters defined for that function.
- Package keyword. If you don't know what packages are, don't worry. You'll learn to love them once you start making bigger Flash programs / class hierarchies.
- Data input and output interfaces used by the new ByteArray data type that provides methods and properties to optimize reading, writing, and working with binary data.

Working at a lower level - exceptions

Exception handling is one area that Flash programmers have been spared thus far. If a number is out of range for the new int and uint data types, an error will be thrown, just like in other languages such as Java. You should do something with these errors and act appropriately or your Flash application could crash. If something failed in AS1 or AS2, the player would nicely ignore it. In Flash 8, you could already see the beginnings of new error handling capabilities if you looked at the new file upload capabilities. This required a whole new set of events for showing upload progress and catching errors that could occur. With AS3, you're at an even lower level.

Exceptions may now occur in several places. For instance: using AS3, you will now need to watch your memory usage more closely. MemoryError is a new exception that is thrown by the AVM2 virtual machine when a memory allocation request fails. Other new exceptions are EOFError, IllegalOperationError, IOError, ScriptTimeoutError and StackOverflowError.

Thinking about these things has formerly been reserved for "real programmers" doing Java and other languages. You can see this from classes such as the StringBuilder. It may remind of the Java class StringBuffer that allows for easier string manipulation. Overall, I must say that the new class structure for the AS3 language looks very good and intuitive. Programmers with a different background will now look at Flash and say "I can understand this". I think we can say that Actionscript is past it's childhood and just finishing off it's teenager years?

New commands!

There is probably more than this, but here are a few brand new commands I found in the AS3 documentation:

Sound
leftPeak / rightPeak Property
The current amplitude of the right channel, from 0 (silent) to 1 (full amplitude).

isBuffering : Boolean [read-only]
Returns the buffering state of external mp3 files.

soundBufferTime : uint
The number of seconds to preload a sound (into a buffer) before it starts to stream.

computeSpectrum()
Click here for an example of using the new sound functions.

System
vmVersion : String [read-only]
The version of the ActionScript Virtual Machine installed on the system.

totalMemory : uint [read-only]
Reports the amount of memory in use currently.
http://livedocs.macromedia.com/labs/1/flex/langref/flash/system/System.html

Debugging
getClassByName(name:String) : Class
Returns a reference to the class object of the class specified by the name parameter.

describeType(value:Object) : XML
Produces an XML object that describes the ActionScript object named as the parameter of the method. This method implements the programming concept of reflection for the ActionScript language.

From this, you can then derive all the info you need about any object in Flash such as: the class of the object , attributes of the class , the inheritance tree from the class to its base classes, the interfaces implemented by the class, the declared instance properties of the class, the declared static properties of the class, the instance methods of the class, the static methods of the class and for each method of the class, the name / number of parameters, return type, and parameter types. Brilliant!

PS: If you've found more new commands, send them to us and we'll add them to this article.

 

Get new stories first

Click to follow us on Twitter!

 

Comments

No comments for this page.

Submit a comment

Only registered members can comment. Click here to login or here to register