Difference between AS2 and AS3

Posted under » Flash on 13 March 2009

In AS 2.0, if I wanted to script a button to take me to a URL, I would simply select the button on the stage and write the following action in the Actions panel:

on (release) {
getURL("http://www.anoneh.com/012.php", _blank);

That's no longer possible in AS 3.0. All scripts now must reside in the timeline or in an external file.

First, we start off by declaring a variable which will hold the URL we want to go to:

var myURLRequest:URLRequest = new URLRequest("http://www.anoneh.com/012.php");

The code is a lot longer than in the previous example, but the result is the same. You can still use buttons in Flash, but here I'm using a Movie Clip object as a button called 'button_mc.' I need to add an event listener to the object. The event listener has two arguments, the type of event and a function that will be triggered when that event occurs. In this case, I'm using a mouse event called click and creating a function called clickHandler.

submit_btn.addEventListener(MouseEvent.CLICK, clickHandler);

The function is pretty simple, although the syntax is very different. Within the funtion we set an event parameter which corresponds to the event in the event listener. Then we use the navigateToURL function with a parameter called link, which looks back at our variable that holds the URL we want to navigate to.

function clickHandler(event:MouseEvent):void {

Although it makes it more OOP as things are function and then you must have an instance before anything can happen, you will find that the code is a lot longer than the AS 2 version which begs the question... wtf?

In AS3, you have to suffer the pain of declaring all variables.

In AS3, there is no onClipEvent.

Next lesson #2 : Sending Variables from AS3 to PHP

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS