Wednesday, 17 June 2009

Open URL in AS3

This is an update tutorial from the previous open URL tutorial. In this Flash tutorial I will teach you how to open a URL in Actionscript 3.0 with a button. I have used an image of ice cream as the button which can be found at: www.sxu.hu

Opening a URL is Actionscript 3.0 requires using a different method from Actionscript 2.0 version. In the AS2, the ‘getURL’ method was used, but this method has now been changed to ‘navigateToURL’ in the AS3 version.

Below is a comparison of the two versions.

Actionscript 2.0:
getURL(“http://www.example.com”, “_blank”):


Actionscript 3.0:
var url:URLRequest = new URLRequest(“http://www.example.com”);
navigateToURL(url, “_blank”);


As you can see the Actionscript 3.0 version needs an addition line of code. I have used the argument “_blank” which open the URL in a new window, but there are other arguments you can use:

* _parent: Loads the parent of the current frame.
* _self: Loads current frame in current window.
* _top: Load top – level frame in the current window.

To open an URL in AS3 you can simply use the method above, but if you are still unsure you can follow the tutorial below.


Open URL in AS3

Step 1 - Create object

Open a new Flash document.
Create an object for button on the stage, or alternatively you can use an image as a button like I have by selecting File > Import > Import to Stage.




Step 2 - Convert to symbol

Convert your object/image into a symbol by selecting F8.
Give your symbol an appropriate name, check button and click ok. You can change the button states if you wish, for more information on buttons checkout the basic button tutorial.


Step 3

Give your button an appropriate instance name e.g. icecream_btn.




Step 4 - Add code

On the timeline insert a new layer called “Actions”. Right click on the 1st frame and select Actions and enter following code:

//1.Button event listener
icecream_btn.addEventListener(MouseEvent.CLICK, openurl);

//2.The openurl function which opens a URL.
function openurl(event:MouseEvent):void {
var url:URLRequest = new URLRequest("http://www.ilike2flash.com");
navigateToURL(url, "_blank");
}

Code:
1. This is an event listener for the button ‘icecream_btn’ which contains a mouse click as the mouse event, and passes the openurl function.
2. This is a function which opens the URL above in a new window. You can change the URL to whatever you wish.

Alternatively you can use one line of code to open an URL.
//1.Button event listener
icecream_btn.addEventListener(MouseEvent.CLICK, openurl);

//2.The openurl function which opens a URL.
function openurl(event:MouseEvent):void {
navigateToURL(new URLRequest("http://www.ilike2flash.com"), "_blank");
}

Step 5

Test your button Ctrl + Enter. Now click on the ice cream and the URL “www.ilike2flash.com” should open.



You should now be able to open a URL in Actionscript 3.0. Feel free to contact me for any questions or comments and remember to subscribe. You can download the source files here.

8 comments:

Anonymous 8 July 2009 09:02  

you forgot the ":" after the http so the code from the ice cream demo kept looking for the site on my local drive. Thanks for the post!

iliketo 8 July 2009 10:04  

@message above

Thanks, I have now added the colon into the code.

littlevietmonkey 16 July 2009 14:38  

is there a way to save a jpeg image via flash (without displaying the image in a web browser)?

iliketo 16 July 2009 16:31  

@littlevietmonkey

I am not sure what you mean by saving an jpeg image.

Joan 1 September 2009 15:29  

Great tutorial! Joan

Ricky 31 October 2010 12:21  

Thanks for the tutorials!really inspiring,I'm just beginning to learn flash,would you suggest a book on beginner's Action Script 3.0 ?thanks a lot !

Unknown 26 August 2012 18:02  

Hi, thanks for this tutorial, I'm doing everything that is here and it was working but then, suddenly stopped, I have my instance named properly the proper AS so not sure what's going on, here's my code but is the same as yours

//1.Button event listener
ElBoton.addEventListener(MouseEvent.CLICK, openurl);

//2.The openurl function which opens a URL.
function openurl(event:MouseEvent):void {
var url:URLRequest = new URLRequest("sponsors.html");
navigateToURL(url, "_blank");
}

Not sure what's going on, any idea? Thanks so much!

iliketo 27 August 2012 10:34  

@Unknown,

Try using a valid url. Also, are you getting any error messages?

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP