Flash AS3 ( actionscript 3 ) Button GetURL

Here’s an example of a simple getURL function in ActionScript 3:
function gotoPage(event:MouseEvent):void
{
var targetURL:URLRequest = new URLRequest(“http://google.com/”);
navigateToURL(targetURL);
}

myBtn.addEventListener(MouseEvent.CLICK, gotoPage);

One thing to remember about AS3 is that it is much more object oriented than  AS2.  Most everything goes to an event listener to be triggered.  Lets break down what the code does:

function gotoPage(event:MouseEvent):void

We are defining our function that we’ve named “gotoPage” and defining the trigger to be a MouseEvent.

 {
var targetURL:URLRequest = new URLRequest(“http://google.com/”);
navigateToURL(targetURL);
}

Now we’re specifying what will happen when the MouseEvent is triggered.  We define that there is a new variable called “targetURL” that hold the URL value.  In this case it is http://google.com
We also specify that the mouse event will trigger a navigation to a url…which value is our variable.

myBtn.addEventListener(MouseEvent.CLICK, gotoPage);

Last of all we define our EventListener that will ultimately trigger our function.  In this case it is a MouseEvent on our button symbol with an instance name of “myBtn”.  It will run our “gotoPage” function when the user CLICKs on “myBtn”.

Back in the olden days of AS2, you would do the above code like so:

myBtn.onRelease = Function(){
getURL(“http://google.com”)
}

I know what you are thinking: so how does AS3 make this easier since I know have to write more code?  The answer is found in the Programming ActionScript 3.0 guide:

ActionScript 3.0 goes beyond the scripting capabilities of previous versions of ActionScript. It is designed to facilitate the creation of highly complex applications with large data sets and object-oriented, reusable code bases. While ActionScript 3.0 is not required for content that runs in Adobe Flash Player 9, it opens the door to performance improvements that are only available with the AVM2, the new virtual machine. ActionScript 3.0 code can execute up to ten times faster than legacy ActionScript code.

So yeah: more robust function scripting capabilities, reusable code bases, performance improvements.  Give it a try and you’ll like it.

Related Posts:

  • No Related Posts

Comments are closed.