Adding code for a clickable button to a URL in Flash

Here's the code:
gotosite_btn.addEventListener( MouseEvent.CLICK, onButtonClick ); 

function onButtonClick ( event:Event ):void {
	navigateToURL(new URLRequest('http://planetoftheweb.com/screencasts'));
} 

Before you do anything, you have to create the button and that it has the instance name: gotosite_btn. If you've got a button with a different name, just change the function name.

Next, we add an event listener to that button. Adding an event listner simply tells the button to wait until something happens. In this case we want the button to look out for when a user clicks on it.

We're passing two things to our listener. First, is what we want it to listen to. In this case, the keyword MouseEvent.CLICK tells the button to listen for a click. The second parameter tells the button that if it receives a click, it needs to execute the onButtonClick function.

So now, we need to write the funcion. which we do in the next part. This function expects an event (in this case our mouse click). When it receives the click, it executes a method. The method is navigatetoURL. That function expects a URL object. so we create it by using the constructor new URLRequest(). Finally we pass the URL we want to that constructor.

It used to be a lot simpler to do this kind of thing in ActionScript 2, but I can see the strength of doing it like this, it's just a bit more verbose and organized.

blog comments powered by Disqus