Flash Buttons with Action Script 3
In this tutorial, RC covers how to create buttons that call to urls using Action Script 3.
This video requires Adobe Flash Player.
Visitor Comments »
Comment by David Miller | July 21, 2008 @ 6:24 pm
Can you zoom in on the other lines of code like you did the first one? Or, post that code on the web/blog site?
Comment by Gary Spedding | July 21, 2008 @ 7:44 pm
Hi Gary,
here’s mine:
btn_layers.addEventListener(MouseEvent.MOUSE_UP, goLayersSite);
function goLayersSite(Event)
{
var site = new URLRequest(”http://www.layersmagazine.com”);
navigateToURL(site);
}
Comment by Steele76 | July 21, 2008 @ 10:35 pm
Hey Steele76:
Thanks for jumping in there! The only thing that I was going to add from the tutorial standpoint is that the instance name of the button is btn1. In that, make sure that the listener is attached to the correct instance name. For example, if you use a buton called btn1: the listener code would be:
btn1.addEventListener(MouseEvent.MOUSE_UP, goLayersSite);
You rock, though..
Comment by RC | July 22, 2008 @ 7:52 am
but… still can’t see the function code.
Comment by jason | July 22, 2008 @ 9:27 am
Seriously… Ya need to be THAT spoonfed? Two people have ALREADY put the code up here, how about just reading?
Comment by adrienne | July 22, 2008 @ 12:20 pm
this is nice. What I’d like more info on is creating button that maintain a down state until another button is pressed. Any suggestions?
Comment by JC | July 22, 2008 @ 5:02 pm
Do you write the Actionscript in the button action. Or in the action layer?
Comment by Rajpal Dayan | August 5, 2008 @ 7:53 pm
spoon feed me. and I second the “zoom in on script.
thanks
Comment by chris | September 3, 2008 @ 1:36 am
I am a beginner in flash help me to improve my knowledge in actionscript i am preparing myself I found it difficult to understand coding
Comment by mahadevan | September 12, 2008 @ 9:37 am
I’ve began my journey to AS3 with this tutorial. Thank man, nicely done and easy to understand
Comment by Jason | September 18, 2008 @ 8:06 pm
Do you really expect rockies like me to follow this, the video window is small and you did not even have the common sense to post a test version of the script. The reason why America is so F-uped is bad teachers like you who turn out students that are not prepared for life and get by with mastering 30%. Shame on you, a nasty shame on you
Comment by Joe Williams | October 1, 2008 @ 4:26 pm
The video window is to small, I advise you to do a text file of the finish action script so I can copy it, can’t quite follow
Comment by Peter Brown | October 1, 2008 @ 4:39 pm
Hey Joe/Peter:
The code is the third comment on this post. Make sure you check it out.
RC
Comment by RC | October 2, 2008 @ 12:32 am
RC - thank you for being so patient with this. I need to point out that the code in the third comment on this post is *different* than what you used in the tutorial. Could you please post an entry with an exact dulplicate of the code you used during the tutorial? Thank you!
Dwayne
Comment by Dwayne | October 2, 2008 @ 2:56 pm
Jason, you didn’t have to come to this site. I guess your personal “Action” script is stuck in an ignorant local view loop. Maybe you should go grab some Flash for Dummies, or a Pocket Manual to Beginners flash if things arent making sense. Its not all the responsibility of the teacher you know, action on your part is the second leg of the race.
Comment by Joseph Orr | October 3, 2008 @ 10:39 pm
Thanks for this, it was a good tutorial. I’m so “green” with Flash and ActionScript. On to my question… how do I add multiple buttons, each with a different URL? I tried making different buttons, each with different instance names, but I keep getting error “duplicate function definition”.
Sorry if this an obvious or stupid question, I just can’t figure it out.
Comment by Stephanie | October 5, 2008 @ 1:01 pm
Forget it! I figured it out! woot!
Comment by Stephanie | October 6, 2008 @ 9:47 am
Attached is the code:
btn1.addEventListener(MouseEvent.MOUSE_UP, goLayersSite);
function goLayersSite(Event:MouseEvent):void {
var layersURL:URLRequest = new URLRequest(’http://www.layersmagazine.com’;
navigateToURL(layersURL);}
Comment by RC | October 6, 2008 @ 10:18 am
Great tutorial and for those who want to be spoonfed…just read a bit more, if you are that lazy don’t bother trying to code, it’s just not for you.
Comment by LArs | October 6, 2008 @ 5:38 pm
how do I add multiple buttons, each with a different URL? I tried making different buttons, each with different instance names, but I keep getting error “duplicate function definition”.
How did you figured it out! Can you help..
Comment by Debok | October 24, 2008 @ 6:36 am
I really need help here.
If I have a button named “MyButton” and I want it to play a Movie Clip on my main timeline called “MyClip”, what is the script I need?
Any help would be appreciated!
Comment by Bill | November 1, 2008 @ 10:50 pm
Being new to ActionScript 3, this is EXCTLY what I needed! Thanks so much for a fantastic tutorial!!
Comment by kim | November 10, 2008 @ 6:03 pm
To DEBOK:
I had the same problem, I figured out that I need to change “goLayersSite” for every button, so for my first button I used “goLayersSite” then in the second button, everytime “goLayersSite” appeared in the code, I just appended a 2 to the end changing it to “goLayersSite2″
third button “goLayersSite3″ etc…here’s the altered code with that in mind (and my own instance names)
btnaboutus.addEventListener(MouseEvent.MOUSE_UP, goLayersSite2);
function goLayersSite2(Event:MouseEvent):void {
var layersURL:URLRequest = new
URLRequest(”about_us.html”);
navigateToURL(layersURL);
}
Comment by Kevin | November 13, 2008 @ 8:23 pm
Does anybody know how to code this so that the browser does not open the link in a new window?
I want the link to open up in the same page that this flash file is contained within.Thanks
Comment by Kevin | November 13, 2008 @ 8:25 pm
navigateToURL(layersURL, “_self”);
Have fun! MAS
Comment by MAS | November 16, 2008 @ 9:31 pm
does anybody know how to create a link between another .swf file using button? (not for web, but for interactive cd)
thx!
Comment by jim | November 20, 2008 @ 7:47 pm
3rd Post
URLRequest(”http://www.layersmagazine.com”);When you give the full url in would it need to be encapsulated in single quotes to make it literal? To keep it from trying to interpret the : in the url.
Comment by BL | November 28, 2008 @ 12:22 pm
Hey. What should i do, if I want to load my URL page in a specific region of my HTML site? I can only figure out how to load the new URL in a new or the same browser
Comment by mathias | December 1, 2008 @ 8:51 am
clearly explaned BUT i couldn’t see what you were writing.
Comment by nick | December 14, 2008 @ 6:06 pm
Hi!
Can you help me!
I’ve made an animated gifbutton in photoshop.
Now I want it to open 3 differents URL when I click the button. Can I do this in Flash?
Can you help me with the code!
Thanks a lot!
I watch you every week on podcast from Sweden!
Comment by Tina Paulsson | December 16, 2008 @ 6:01 am
This has been really helpful. I thank you guys!
Comment by zippan | December 28, 2008 @ 11:46 am
Hi,
Can you advise me how to add an expanded navigation list for each button when in “mouse over”.ie;
There are 8 navigation buttons visible on the index page. I would like the buttons to display several navigation options when individual buttons are in “mouseOver” state. Giving the user perhaps 2 or more options per button.
…so in other words one button becomes, let’s say 5 navigation choices.
Complicated?
Thanks for all help!
Comment by Bill | January 4, 2009 @ 1:56 am
But if you wanto do this framevise.
I have a button that i want to go to, lets say, frame 34. How do i write this?
Comment by Lasse | January 18, 2009 @ 3:50 pm
Hey Lasse,
Name the frame by clicking on it and then typing in an instance name. For this example, i’ll use “frame34″ as the instance name of the frame and “mybutton” as the instance name of the button.
function goframe34 (e:MouseEvent):void{
gotoAndStop(”frame34″);
}
mybutton.addEventListener(MouseEvent.CLICK, goframe34);Hope this is what you were after.
Comment by Kimberley | February 10, 2009 @ 5:21 pm
I want to use a button to unload a movie previously loaded on level 2 of my master swf file. How do I do this in AS3/CS4?
I’ve tried saving an old swf file with AS2 preferences in CS4, but it doesn’t work. Actually, the old files still work, as long as I don’t open them & save in CS4. It doesn’t seem to matter what publishing preferences I choose. So it looks like I’m going to need to get friendly with AS3 right away.
Thanks for all of your hard work!
Comment by Charley | February 10, 2009 @ 11:56 pm
It’s not working for me I keep getting syntax errors on these lines
btn_layers.addEventListener(MouseEvent.MOUSE_UP, goLayersSite);
function goLayersSite(Event)
{
var site = new URLRequest(”http://www.uvreactive.com”);
navigateToURL(site);
}
Comment by Parkaboy | February 19, 2009 @ 10:24 am
I am pretty new to Actionscript so far to say I can set up very basic “Event Listeners” to run movie clips etc.
My current project contains a self designed universal power button created originally in Photoshop which I use to fire up a few simple actions.
1. Off 2. Rollover and the colour changes 3. The buttons rollover colour remains but looks as if it’s been pressed. Pretty simple stuff.However on mouse out, I would like the button to return and stop at the rollover stage (which looks as if the powers on) not the up (off stage).
This button only needs to act once to start the project and never be used again as the site menu kicks in.
example: http://www.pwcreativedesign.co.uk
Comment by Peter Wesseldine | February 21, 2009 @ 12:48 pm
MAS,
Dude, I need you help i’m having the same prob as Kevin (who’s post you replied to) and you reply while a valid hypothesis didn’t work for me for some strange reason, on the contrary when i click the button on the intro SWF file i expect the result to take me to my main site however it does absolutly nothing. So I was wondering if you could help me out.Cheers Mate
Comment by David | February 24, 2009 @ 6:52 am
For those of you who might be having syntax problems using CS4 and Actionscript 3 when trying to implement this code, this might be of some help. I was getting syntax errors on this line:
URLRequest(”http://www.layersmagazine.com”);
but after snooping around and trial and error I found that I needed to replace the ” ” quotation marks with ‘ ‘ apostrophes.
The whole URL will turn GREEN when its correct.
Hopefully this will work for you. I wasted almost 4 hours of my life to get that fucker working!
![]()
Comment by Very relieved man! | March 4, 2009 @ 12:17 pm
here is my code but i still cant get it to work
can anyone tell me why?
btn1.addEventListener(MouseEvent.MOUSE_UP, goThomasfranksSite);
function goThomasfranksSite(Event:MouseEvent):void {
var ThomasfranksURL:URLRequest=new URLRequest ;
“http://www.thomasfranks.co.uk”;
navigateToURL(ThomasfranksURL);
}
Comment by jamie Burrell | March 5, 2009 @ 11:11 am
Hey Guys:
Thanks SOOO much for the Single quotes thing. Sometimes when I am typing out pretty fast, the little things get mucked up.
BL, and several others are absolutely correct. You need the URL in SINGLE QUOTES for it to work. So, if we take Jamie’s example:
btn1.addEventListener(MouseEvent.MOUSE_UP, goThomasfranksSite);
function goThomasfranksSite(Event:MouseEvent):void {
var ThomasfranksURL:URLRequest=new URLRequest ;
‘http://www.thomasfranks.co.uk’;
navigateToURL(ThomasfranksURL);
}Thanks for keeping me honest over here..
![]()
RC
Comment by RC | March 5, 2009 @ 11:28 am
Thanks for that RC
but ive placed that in and when testing this keeps coming up
TypeError: Error #2007: Parameter url must be non-null.
at global/flash.net::navigateToURL()
at buttontest2_fla::MainTimeline/goGoogleSite()what does this mean?
Comment by jamie Burrell | March 5, 2009 @ 11:58 am
Thanks guys this has been a bitch !!! hate the new AS3, still cant get this to work properly, now my stop action on a movie is not working since i added this code
enterbt.addEventListener(MouseEvent.MOUSE_UP, goLayersSite);
function goLayersSite(Event)
{
var site = new URLRequest(”http://www.lyuy.com”);
navigateToURL(site);
}i have tried using this.stop but it keeps looping
Comment by r0m3 | March 12, 2009 @ 9:29 pm
Thank you for the awesome tutorial, this along with Flash CS4 For Dummies should make me relatively useful in this field. YAY! *Bookmarks on tab bar*
Comment by Craig | March 16, 2009 @ 6:32 pm
Yeah I keep getting the syntax error too. I tried it with the apostrophes as well as with the quotation marks and neither work for me.
1084: Syntax error: expecting rightparen before colon.
Comment by jason Ott | March 17, 2009 @ 5:34 pm
I get similar error messages in Flash CS4 if the url is in double quotes I get an 1804 compiler error and if I go to single quotes I get a different error.
TypeError: Error #2007: Parameter url must be non-null.
at global/flash.net::navigateToURL()”http://www.google.com”I am brand new to any type of flash but this seems extremely hard to get a simple click to go to a URL
any help appreciated.
Comment by steve Monty | March 24, 2009 @ 5:25 pm
got it after playing for a while, need a second pair of () in the code see below - change home to whaterever your instance is called. All I need to do is to get it to open the web page in the same page as the button was on.
home.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.google.com/”));
}
Comment by steve Monty | March 24, 2009 @ 5:56 pm
can anyone help me to get the button when clicked to open in the same page as the button ? I want to put a navigation bar with 5 buttons onto my web page, I tried the _self tag but it will not work in the above script - I tried
home.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.google.com/”,”_self”));
}but only got a syntax error
Comment by steve Monty | March 24, 2009 @ 6:00 pm
Jamie:
Try this:
btn1.addEventListener(MouseEvent.MOUSE_UP, goThomasfranksSite);function goThomasfranksSite(Event:MouseEvent):void
{
var ThomasfranksURL:URLRequest=new URLRequest(’http://www.thomasfranks.co.uk’);
navigateToURL(ThomasfranksURL);
}
Comment by Newbee 007 | March 26, 2009 @ 9:08 pm
HELP! No matter witch site I try i get this message in the “OUTPUT” bar.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at AL_fla::MainTimeline/frame1()
Comment by andreas | March 27, 2009 @ 1:16 pm
Hello all,
I am very new at flash.
I have a problem i have been working for hours, obsessed about getting this problem solved. No avail, help.I need a button to go to a URL, and you have the AS, but when I tried this it does not work for me.
I took your code and put in my stuff and it still does not work. This is what I did.
btn.addEventListener(MouseEvent.MOUSE_UP, goMidniteoilSite);
function goMidniteoilSite(Event:MouseEvent):void {
var MidniteoilURL:URLRequest=new URLRequest;
‘http://www.midniteoil.com/NEW/logos.html’;
navigateToURL(MidniteoilURL);
}I am getting a syntax error on this line ‘http://www.midniteoil.com/NEW/logos.html’;
This all started when I found a flash intro I liked and went bought it and played inside and stuff. It was created with Flash 6 in AS1. I am working inCS4 and when I wanted to ad this button in AS3 it told me actions will not work from that bought intro flash. This also took a while to figure out.
In process of elimination let me try a button in straight CS4 without anything from the Flash 6 and the code is still not working.
I followed RC’s tutorial and still nothing. Can you folks help me so I can get my life back. Appreciate all the help.
Comment by markm | March 30, 2009 @ 12:13 am
I have tried every single code listed here in Flash CS4…all I want is for my button to open another flash html file I have created…this code worked beautifully on a previous project I did in CS3…but I get all kinds of syntax errors in CS4. What gives?
Comment by Leah | March 30, 2009 @ 9:17 am
Nevermind!
I got it to work….and I got the url to open in the same window. The code is as follows:
homebutton.addEventListener(MouseEvent.MOUSE_UP,goLipstickSite);
function goLipstickSite(Event:MouseEvent):void {
var LipstickURL:URLRequest= new URLRequest(”LipstickStilettosSite.html”);
navigateToURL(LipstickURL,”_self”);}
**There must be a space between =, new, and URLRequest
the quotation marks WILL work…(they did for me anyway) you don’t have to use apostrophes. The _self tag also worked wonderfully. Thank you so much RC and everyone else!![]()
Comment by Leah | March 30, 2009 @ 9:37 am
Has anyone concluded (like I have) that AS3 is a total hassle and waste of time??
Before, all you needed was:
on (release) {
getURL(’http://www.yahoo.com’);
}THAT’S IT! YOU WERE DONE! What’s with all this ‘function’ nonsense, and variables, and listeners, and gooblygocks and dorkyMcfuzzes?
Thanks a lot ADOBE. Clients love it when we take 4X as long to do a simple task due to RE-learning shit we already knew.
Comment by artbum | April 2, 2009 @ 9:19 pm
Hi Actionscripters.. Im a designer struggling with a basic AS3 task…I cannot get rid of this Error #1009, driving me nutts!
I want to use a series of buttons to link to different URLs. If i remove the “btnPrint” instance name, i can get this code to work for 1 button. But add the instance name back in, or try to add this code for more than 1 button, and up comes the Error #1009.
here’s my script:
// print button
btnPrint.addEventListener(MouseEvent.CLICK, callLink);
function callLink(event:MouseEvent):void {
var url:String = “printing/”;
var request:URLRequest = new URLRequest(url);
try {
navigateToURL(request, ‘_parent’);
} catch (e:Error) {
trace(”Error occurred!”);
}
}// photo button
btnPhoto.addEventListener(MouseEvent.CLICK, callLink2);
function callLink2(event:MouseEvent):void {
var url2:String = “photography/”;
var request:URLRequest = new URLRequest(url2);
try {
navigateToURL(request, ‘_parent’);
} catch (e2:Error) {
trace(”Error occurred!”);
}
}Testing the script shows no errors, and there are no errors when publishing. I only get this error #1009 when Flash Player has played the swf file.
Ive checked my instance names and they are all there and have no typos. This script is on its own frame. Is there anything obvious Im doing wrong?!
Please help!
Comment by markyb | April 7, 2009 @ 10:35 pm
…and I have just changed my script to use to single quotation marks; does not solve my problem
Comment by markyb | April 7, 2009 @ 10:39 pm
Hello there.
I’m a new actionscript user. I can do very, very basic stuff. I do use flash itself VERY often and I’m fairly good (if I may say so). My problem is very much like what some of you have. I created a flash website and on the one side I have my buttons. The problem is I don’t know what to do now!! Where do I go and what do I type? Do I need to define instance names for each button before I start the action script?
PLEASE HELP!! Thanks
Comment by Rudi | April 28, 2009 @ 9:00 am
Well, I read through all of the above and I keep getting this error - 1061: Call to a possibly undefined method addEventListener through a reference with static type Class.
Am using CS4 adn this is my code…
button.addEventListener(MouseEvent.MOUSE_UP, goLayersSite);function goLayersSite(Event)
{
var site = new URLRequest(’http://www.xaverian.org’);
navigateToURL(site);
}Have even tried (Event:MouseEvent):void
Comment by John BianRosa | April 28, 2009 @ 10:53 am
Never Mind. thanks anyway. I changed the name ‘button’ to ‘btn1′ and it worked. Guess ‘button’ is a reserved word?
Comment by John BianRosa | April 28, 2009 @ 12:07 pm
THANK YOU!!!!
I have done so much research in books and on the web trying to find out how to add a llink to a web banner. You would think its a big secret or something as difficult it is to find out how to do it. This is the first turtorial that actually explains the process and the code works! Thank you so much.
Comment by Jenny | May 4, 2009 @ 4:14 pm
Brand new CS4: copied tutorial same to same. click on button does not open any browser whatsoever. Are there any other environmental settings like permissions blah blah?
Comment by www.simianart.com | May 5, 2009 @ 6:51 am
AAH! I found it… when I saved the .fla in D:\ drive, it is failing. But when .fla is saved in “My Documents” (OS is on C: drive) it works!!! WTF!!!
Comment by www.simianart.com | May 6, 2009 @ 1:08 am
Thank you RC for your tutorial and clarifications notes. I appreciate the few people like you out there that takes the time to teach us.
About some commentators in this blog, I find disconcerting those of you that made some nasty comment in this blob, is it some kind of power trip? Were is your empathy(the ability to “put oneself into another’s shoes,” in case you forgot)? People come from many different places and may or may not be prepared to tackle the problem at hand, but the mere fact that they posted their question no matter how dumb it may appear to you should deserve some respect. Be cool…And for some of you who critic the teacher why don’t you try teaching see if you can do better. I think constructive criticism is always welcome by anyone but insult never helps it is upsetting and is a waist of the reader’s time and yours. Be cool too.
Comment by gama | May 7, 2009 @ 6:57 am
Getting error messages on multiple buttons:
ArgumentError: Error #1063: Argument count mismatch on flash.net::URLRequest(). Expected 0, got 2.
at awd_fla::MainTimeline/goInteractiveHandler()What am doing wrong?
intr_btn.addEventListener(MouseEvent.MOUSE_DOWN, goInteractiveHandler);
function goInteractiveHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”interactive”,”_self”));
}
prt_btn.addEventListener(MouseEvent.MOUSE_DOWN, goPrintHandler);
function goPrintHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”print.html”,”_self”));
}
id_btn.addEventListener(MouseEvent.MOUSE_DOWN, goIDHandler);
function goIDHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”identity.html”,”_self”));
}
abt_btn.addEventListener(MouseEvent.MOUSE_DOWN, goAboutHandler);
function goAboutHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”about.html”,”_self”));
}
cont_btn.addEventListener(MouseEvent.MOUSE_DOWN, goEmailHandler);
function goEmailHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”mailto:work@andrewwilsondesign.com”,”_self”));
}
Comment by Andrew Wilson | May 12, 2009 @ 2:22 pm
Hey RC - thanks for the video - it’s helping me a lot.
I have two questions if you are able to help a little more…
1) I notice there are property type windows down the bottom of the screen where you added the instance name. My workspace doesn’t have them, and I was wondering how to bring them up?
2) the timeline for my button symbol just has ‘layer 1′, no ‘action layer’. How do I make an action layer?
Cheers
Shaun
Comment by Shaun Thomson | May 26, 2009 @ 7:34 am
Great tutorial!
Got it how to do it before it even ended!
Thanks a lot!
Comment by André Guerra | June 11, 2009 @ 12:28 pm
[quote]Comment by jason | July 22, 2008 @ 9:27 am
Seriously… Ya need to be THAT spoonfed? Two people have ALREADY put the code up here, how about just reading?
[/quote]Look, jason, I used to be pro at AS2 with Flash 8, but now with this cs3/4 I can’t even add actionscript to a button. It’s pretty confusing going with this change, sorry if it bothers you. (not)
Comment by \_- UAI -¯\ | June 11, 2009 @ 12:49 pm
Great tutorial, but I’d like to load the html page into a certain area of the flash page–or maybe into an iframe–is there a way to do that via flash?? I’d so appreciate some help. Thank you!!!
Comment by Jamie | June 29, 2009 @ 1:16 am
Leave us a comment

- Using and Aligning Layers to a Guide
- Snapping Layers to Each Other
- Simulating a Polarizing Filter for Skies
- Quick Zoom Shortcuts
- Moving Selections, Not the Layer





Photoshop
Illustrator
Indesign
Dreamweaver
Fireworks
Premiere
Flash
After Effects
Lightroom
Acrobat







