How to Embed a Link Into a Button in AS3

Linking with buttons in your flash game to another website will give you more Web and game design flexibility. With an embedded link in your Flash animation, you can include ads or let players select different games based on a wide range of criteria. With ActionScript 3 (AS3), you can even embed specific links in buttons based on a game score. Embedding links in buttons can be done with just a few lines of code. However, you should also add code to display a message if the Web link is presently not accessible.

Things You'll Need

  • Adobe Flash Professional (CS3, CS4 or CS5 Versions)
Show More

Instructions

    • 1

      Start the Flash program. Click "New" from the "File" menu on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0)" from the "New Document" dialog box that appears. Click the "OK" button to close this dialog box.

    • 2

      Select the "Actions" option from the Window menu on the main Flash menu bar to open the "Actions" editor. Position your mouse cursor on the first line of the "Actions" editor. Click the mouse button and type in the following code to declare a MovieClip object named "mybutton":

      var mybutton:MovieClip = new MovieClip();

    • 3

      Type the following code on the next line of the "Actions" editor to instruct Flash to draw a rectangular red button with a width of 50 pixels, a height of 20 pixels and rounded corners (10 pixels):

      mybutton.graphics.beginFill(0xFF0000);

      mybutton.graphics.drawRoundRect(200,200, 50, 20, 10);

      mybutton.graphics.endFill();

    • 4

      Type the code below on the next line in the "Actions" editor to instruct Flash to place the button on the stage when the flash movie plays:

      addChild(mybutton);

    • 5

      Type the code below on the next line in the Actions editor to instruct Flash to link to the Example.com Web page and display the Example.com Web page in the browser when the button is clicked, or to display the message "The Link is not active at this moment, try later" if a network connection cannot be made to the website.

      mybutton.addEventListener(MouseEvent.CLICK, myLink);

      function myLink(e:MouseEvent):void {

      var urlname:String = "http://www.example.com/";

      var sendrequest:URLRequest = new URLRequest(urlname);

      try {

      navigateToURL(sendrequest);

      } catch (e:Error) {

      trace("The Link is not active at this moment, try later");

      }

      }

    • 6

      Copy and paste the following code into the "Actions" editor to ensure that no errors are present:

      var mybutton:MovieClip = new MovieClip();

      mybutton.graphics.beginFill(0xFF0000);

      mybutton.graphics.drawRoundRect(200,200, 50, 20, 10);

      mybutton.graphics.endFill();

      addChild(mybutton);

      mybutton.addEventListener(MouseEvent.CLICK, myLink);

      function myLink(e:MouseEvent):void {

      var urlname:String = "http://www.example.com/";

      var sendrequest:URLRequest = new URLRequest(urlname);

      try {

      navigateToURL(sendrequest);

      } catch (e:Error) {

      trace("The Link is not active at this moment, try later");

      }

      }

    • 7

      Click the "TestMovie" option in the "Control" menu on the main Flash menu bar to play the movie and test the link that is embedded into the button. Position your mouse over the button after the movie begins playing. Verify that the example website appears in your browser after you click the button.

Learnify Hub © www.0685.com All Rights Reserved