Linking to URL in Action Script 3

geturlLinking to URL in Action Script 3

Introduction

The way Action Script deals with events on buttons and movie clips is totally different than the old one we used to do with Action Script 2, but generally it is more professional and saves time and effort if you understood it well.

The new Action Script 3 is showing a new trend on the Action Script language by moving to the Object oriented programming concept. However, in this article, I will show the new method to create a link to URL in Action Script 3 file.

Requirements:

Adobe Flash CS3 professional

Sample files01.zip

To complete this tutorial, you will need basic knowledge of using Adobe Flash CS3 Professional.

Create your button:

First, we will create a button on the stage, and give it an instance name like following:

1- Create a button on the stage.

2- From the properties panel, give your button an instance name in the instance name field. Here, I named the button “button_mc”.

Adding the Action Script

As we said in the last article, the new Action Script 3 does not allow you to add action on movie clips or buttons as we used in Action Script 2. However, we will create a new layer and name it Action Script to add the button Action as following:

1- Create a new layer and name it Action Script

2- Press F9 to access the Action panel

3- In the Action panel, we will add the action as following:

a. At the first link we will add a variable that will contain the URL we want to link to. Here we will add a variable named “link” and it will include the URL on Graphic Mania Site ” www.graphicmania.net “. So the code of the first line will be like following:

var link:URLRequest = new URLRequest (“www.graphicmania.net”);

b. In the second line, we will add an event listener that will make the button listen to the user click like following:

button_mc.addEventListener(MouseEvent.CLICK, gooo);

c. After adding the event listener, we will create the function that will show the button what to do once the user click on it.

function gooo(event:MouseEvent):void { navigateToURL(link); } The function, which we named it “gooo” tells the button to navigate to the URL stored in the link variable we declared in the first line of code.

However, the full code we will ad is as following:

var link:URLRequest = new URLRequest (“www.graphicmania.net”);

button_mc.addEventListener(MouseEvent.CLICK, gooo);

function gooo(event:MouseEvent):void {

navigateToURL(link);

}

Where to go from here

The differences between the old AS2 and the new AS3 are huge, but it really deserves to learn, it will give you more capabilities and more flexibility. If you are a designer that seeks a better performance to your work and more capabilities, then AS3 will give you what you need and more.


Tags: , ,

Category: Flash tutorials

About the Author ()

Rafiq Elmansy is a graphic designer and runs his own design studio Pixel Consultations. He is also an Adobe Community Professional, Certified Expert and Adobe user group manager. He is a Friend of Icograda (the International Council of Graphic Design Associations).You can read his writings on Adobe site, Adobe Edge magazine, communitymx.com and his own blog www.graphicmania.net. He can also be followed on Twitter @rafiqelmansy

Comments (18)

Trackback URL | Comments RSS Feed

  1. Yehia says:

    Glad to see more arab dudes jumping into AS3/Flash. The tutorial is pretty organized. Good job man.

  2. Yehia says:

    Glad to see more arab dudes jumping into AS3/Flash. The tutorial is pretty organized. Good job man.

  3. Yehia says:

    Glad to see more arab dudes jumping into AS3/Flash. The tutorial is pretty organized. Good job man.

  4. Yehia says:

    Glad to see more arab dudes jumping into AS3/Flash. The tutorial is pretty organized. Good job man.

  5. Thanks alot Yehia and happy you liked it ;)

  6. Thanks alot Yehia and happy you liked it ;)

  7. Thanks alot Yehia and happy you liked it ;)

  8. gabe says:

    thanks for the tutorial! My linkings are working now. BIG THANKS!

  9. gabe says:

    thanks for the tutorial! My linkings are working now. BIG THANKS!

  10. gabe says:

    thanks for the tutorial! My linkings are working now. BIG THANKS!

  11. gabe says:

    thanks for the tutorial! My linkings are working now. BIG THANKS!

  12. Adamb says:

    thanks dude great help!!

  13. apple says:

    how can i make the link to open in parent page? Not like a new window kind of thing? Not really a actionscript user, need some help here.. thanks..

  14. Steve says:

    Thanks Thanks Thanks dude !

  15. Steven says:

    hi, thanks for your help! But, how to add in “_blank ” in actionscript 3? i want open URL in new window. pls help! thanks

  16. any query knock me……..

  17. Shashank says:

    Hey Thanks a lot man! You saved my time by the sample file. Thanks again.

  18. C.N. says:

    The code is fine, but the function MUST be declared first, and afterwards add the EventListener to the button

    So,

    var link:URLRequest = new URLRequest (“www.graphicmania.net”);

    function gooo(event:MouseEvent):void {
    navigateToURL(link);
    }

    button_mc.addEventListener(MouseEvent.CLICK, gooo);

Leave a Reply