Wednesday, June 29, 2011

Actionscript 3 - Play and URL button

IT'S AS SIMPLE AS COPY AND PASTE THE CODE BELOW!

I spent about 4 hours this morning trying to find a code for 2 simple buttons. One to link to another frame, and another to load a URL. I was so used to the AS2 "GotoAndPlay" or "GetURL" commands that AS3 really seems to unnecessarily confusing for the average animator.
No matter how many codes I copy/pasted, I kept getting syntax errors or whatever.
Finally I found ones that work, so to save you all the trouble, here they are.

To link to another frame within the same flash animation:


For this code to work is to have the INSTANCE name of your button set as 'playBtn'. You can do that by selecting the button that is on your frame, going into properties, and changing it in the instance name text box.
The code below must be pasted on the FRAME that your button is in, not the button itself. To change which frame this button goes to and plays, just change the number from '2' to whatever frame you wish to be played.

playBtn.addEventListener(MouseEvent.CLICK, playmov);
function playmov(evt:MouseEvent):void{
gotoAndPlay(2);
}
To link to a URL which will load in a new window:

For this code to work is to have the INSTANCE name of your button set as 'nolink'. You can do that by selecting the button that is on your frame, going into properties, and changing it in the instance name text box.
The code below must be pasted on the FRAME that your button is in, not the button itself.
Also just change the URL in the code to whatever URL you wish to load


nolink.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.noemuproductions.com/"));
}

More help:
 To stop your animation on the frame where the button is, so that the viewer must press the button to continue the animation, simply write
stop();
on the frame you wish the animation to stop on. This should come before the code for the button. For example, the URL button with stop code would be:

stop();
nolink.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.noemuproductions.com/"));
}

and the go to different frame button with stop code would be:

stop();
playBtn.addEventListener(MouseEvent.CLICK, playmov);
function playmov(evt:MouseEvent):void{
gotoAndPlay(2);
}


If you are wishing to incorporate BOTH buttons as well as a stop(); command, copy the code below:

stop();
nolink.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
playBtn.addEventListener(MouseEvent.CLICK, playmov);

function mouseDownHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.noemuproductions.com/"));
}

function playmov(evt:MouseEvent):void{
gotoAndPlay(2);
}


I hope this helps!

1 comment: