Monday, September 15, 2008

Getting a button to work in Flash using ActionScript 3 - Simple code insert

I'm relatively new to Flash and ActionScript but I've always known that button aren't as simple as they should be.

I started using ActionScript 3 for the first time today it turns out that GetURL no longer exists! But i found the following works fine.

This is a basic run through, it assumes that you already know a little about flash, I hope it helps...
  1. Create a button graphic and convert it into a button - if you want it to be clear i just set the Blend mode to Alpha... this seemed to work fine!
  2. Name the button - in this case "buttonName".
  3. Drop the following code into the first frame of your timeline (into either a new layer or your actions layer if you are familiar with this way of working).
    buttonName.addEventListener(MouseEvent.CLICK, onMouseClick);

    function onMouseClick(e:MouseEvent):void
    {
    var request:URLRequest = new URLRequest(“http://www.urlhere.com”);
    navigateToURL(request, “_blank”);
    }
  4. Change the URL and buttonName to suit and remove the target _blank line if not required.
  5. Test the movie.
  6. Done.
Thanks to School of Flash for this one.

No comments: