Adding more than one button to a FLA file while rocking it in ActionScript 3.0

by Jen deHaan

So there have been a number of questions on my kinda recent post about making a button in Flash CS3 or Flash CS4 using ActionScript 3.0. I will do the next couple posts on how to do the things in the questions (and actually check em out in Flash, and make an FLA to download). Update: More links about buttons and button code at the end of this post.

So if you haven’t already read or don’t know how to make a single button, head on over to the post linked above. Here, I’ll continue on to multiple buttons.

Say you have a couple buttons on a page, and you want one to go to one URL, and the second one to go to a different URL – you need to change your function names in your code so you don’t have multiples with the same name. So, you would need to do something like this:

thumbsdown_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbDownHandler);
function thumbDownHandler(event:MouseEvent):void {
 navigateToURL(new URLRequest("http://msdn2.microsoft.com/en-us/silverlight/default.aspx"));
}
thumbsup_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbUpHandler);
function thumbUpHandler(event:MouseEvent):void {
 navigateToURL(new URLRequest("http://www.adobe.com/devnet/flash/"));
}

Note the thumbDownHandler and thumbupHandler in the code.

The source file: Mutliple buttons to URLs

So some people are instead targeting multiple frames. In that case, the code would look like this:

stop();
first_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void {
 gotoAndStop(5);
}
 
second_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
function mouseDownHandler2(event:MouseEvent):void {
 gotoAndStop(10);
}

The source file: Mutliple buttons to frames

MORE INFORMATION ON BUTTONS:


132 Comments to “Adding more than one button to a FLA file while rocking it in ActionScript 3.0”

  1. Hi, I’m new to Flash, and having a real problem.

    Basically, I’ve got a simple flash file that has 5 buttons that use a motion tween (I want them to come in from off screen to their final positions). That bit works, no problem.

    However, What I’m trying to do after the motion stops is to make each button do display some pop up text. The animation goes over 49 frames, and each button is on a seperate layer, named similar to the button. I’ve also got a layer called actions. All the layers run until the end of the scene at frame 49, so all buttons are visible at the end.

    The project looks like this:
    sizeLayer
    Toughnesslayer
    Talktimelayer
    etc.

    There is an instance of a button on each layer,
    size_button on sizeLayer
    Talktime_button on talktimeLayer

    I’ve added the following code to frame 49 of the actions layer

    stop();

    this.size_button.addEventListener(MouseEvent.MOUSE_DOWN, sizeHandler);
    function sizeHandler(event:MouseEvent):void {
    trace(“I’ve been clicked!”);
    };

    but when it runs, I get an error:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Test_fla::MainTimeline/frame49()

    (Test.fla is the filename I’m working with at the moment).

    Any ideas as to how I might get this working please. I’ve been at it for about 5 hours now and it’s frustrating.

    Thank you.

  2. Thanks for this lesson! AS 3.0 is whacked.

  3. Hi:

    Similar to Anita who posted a problem with her Action Script on March 30th, 2008 at 12:35 am.

    her code was:

    I’ve also tried this, and have sort of the same kind of problem with one of the buttons not working. This is my code:

    maria.addEventListener(MouseEvent.CLICK, gotomaria);
    function gotomaria(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.fef-aalborg.dk/testsite/sider/rapport/indien2008maria.html”), “_self”);
    }
    lisser.addEventListener(MouseEvent.CLICK, gotoingerlise);
    function gotoingerlise(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.fef-aalborg.dk/testsite/sider/rapport/indien2008ingerlise.html”), “_self”);
    }

    Both buttons, for some mysterious reason, go to the same page (the first URL specified), and I cannot find out what I’m doing wrong. I didn’t name anything the same, and I tried copy/paste to find out if I had overlooked an error in button two, but no. Help… :-)

    and you suggested that if she change the url to seperate sites that that might work.
    By the way is this as2 or as3?

    I am having the same problem, except I have different URL’s and it does not seem to work for me. First of all the code is applied on in the time line, in a image handler that has 24 images and text and multiple layers in the timeline, each with it own movieclip and inside that a button. (when the user clicks the images, which scroll from left to right, it shows a larger thumbnail of the picture, above the image scroller, and then when the user clicks this image it is supposed to take them to the site I want them to go to) as I said it keeps taking them to the same site, even images that I have not assigned any Actions too, they also link to the same site.

    here is the code.

    Images_btn1.onRelease = function() {
    aaronshust()
    }
    function aaronshust(){
    getURL(“http://www.aaronshust.com/index.html”, “_blank”)
    }

    and

    Images_btn2.onRelease = function() {
    matthewwest()
    }
    function matthewwest(){
    getURL(“http://www.matthewwest.com”, “_blank”)
    }

    but as i said the both go to the first site.
    as I said their a 24 separate images that i need to point to 24 sites, my deadline in the end of October here.

    So My question is are.

    In AS 2.0

    My directory tree is images/ holder/ Image_Text (each with an _1, _2,_3 ETC.. to _24 for example Image_Text_1)/ and Images_btn1 Which is where I added the action script in the time line

    1. Do I need to create the code on the on the holder level?
    2. if so what would the code be so that when the user clicks on each separate images it will go to the proper sites?
    3. Can I assign different variables to the code for each image and call each one using a function, if so what is this code.
    I am still new to learning AS.

    I have looked all over the net to find a solution, please help

    Lee

  4. Great resource!
    Jen, you’re awesome.
    I have a small issue with buttons in that I have some buttons that go to frames within flash and others that go outside to different urls. The problem is if I click on the button that goes to a different frame, I can’t click the button that goes to a different URL. I have to refresh the page first.
    Is there something I should put in the code to refresh the page?
    thanks!

  5. I’m trying to set up a “Movieclip _mc” style button and I can’t seem to find a tutorial on how to point this action to an external website.
    Every article I read is about the _btn class!!!!

    Frustrated Eric

  6. Hey guys i have some trouble getting some buttons to work…
    Inside my buttons I have movie clips, which keep the buttons rotating until you click,
    my problem is that I need the code for the CLICK… when you click they don;t stop!

    I have tried several things but they won;t stop.. does any body know?

  7. hey, I tried using your help from what you gave Chris, in the 2nd post… but even with your code, the button links keep opening up in new windows…

    here’s my code:

    home_btn.addEventListener(MouseEvent.MOUSE_DOWN, buttonDownHome);
    function buttonDownHome(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.web4brains.com/barcode/”),”_self”);
    }
    creators_btn.addEventListener(MouseEvent.MOUSE_DOWN, buttonDownCreators);
    function buttonDownCreators(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.web4brains.com/barcode/creators”),”_self”);

  8. oops nvm… must have been an uploading/saving problem.. thanks!

  9. Thanks for the support. It’s a pity that Adobe’s support couldn’t be as clear.

  10. Hi Jen,

    I have a flash movie, that I’ve got 4 buttons on a page. I can get the first button, and the last button to work, but for some reason I cannot get the middle two buttons to work. When I test the movie, it runs fine, the two middle buttons(Contacts and Link) do nothing when you click on them. The top button, gallery and the dynomite button work correctly. Here is my code:
    function handleClick1(pEvent:MouseEvent):void
    {
    if( pEvent.target == gallerybtn )
    {
    //handle the event
    gotoAndPlay (1, “Gallery”);
    }
    function handleClick1(pEvent:MouseEvent):void
    {
    if( pEvent.target == contactbtn )
    {
    //handle the link event
    gotoAndPlay (1, “Links”);
    }
    function handleClick1(pEvent:MouseEvent):void
    {
    if ( pEvent.target == linkbtn )
    {
    //handle the contact event
    gotoAndPlay (2, ” Contacts” );
    }
    function handleClick4(pEvent:MouseEvent):void
    {
    if( pEvent.target == dynomitebtn )
    {
    //handle the dynomite event
    gotoAndStop (60, “Webpage Set”);
    }
    }}}}
    //register events
    gallerybtn.addEventListener(MouseEvent.CLICK, handleClick1);
    contactbtn.addEventListener(MouseEvent.CLICK, handleClick1);
    linkbtn.addEventListener(MouseEvent.CLICK, handleClick1);
    dynomitebtn.addEventListener(MouseEvent.CLICK, handleClick4);

    If I change the contact and link events to handleclick 2 I get “error 1120 Access of undefined property handleClick 2,
    handleClick3″

    Any help you may begin to offer would be excellent!
    Thanks,
    Todd

  11. I keep getting a error 1120: Access of undefined property btn4Handler. Below is the code I am using.

    Any help to solve this would be appreciated.

    btn1.addEventListener(MouseEvent.MOUSE_DOWN, btn1Handler);
    function btn1Handler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.s2sbuilders.com”), “_blank”);
    }

    btn2.addEventListener(MouseEvent.MOUSE_DOWN, btn2Handler);
    function btn2Handler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.s2sbuilders.com/prodects.htm”), “_blank”);
    }

    btn3.addEventListener(MouseEvent.MOUSE_DOWN, btn3Handler);
    function btn3Handler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.s2sbuilders.com/photos.htm”), “_blank”);
    }

    btn4.addEventListener(MouseEvent.MOUSE_DOWN, btn4Handler);
    function btn4andler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.s2sbuilders.com/quotes.htm”), “_blank”);
    }

  12. I have an flash file with 4 scenes. In layer 2 i have a movieclip. In layer 1 i have 4 of the buildt in buttons which you can find under components. I want this 4 buttons to go to 4 seperate scenes. How do i do it?
    Should i use this code?

    }

    b80.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler5);
    function mouseDownHandler5(event:MouseEvent):void {
    gotoAndStop(“Scene 1″);
    If its correct where shall i but it because direct on the button you cant have a action.
    //Pontus

  13. After long hours and numerous tries, tons of tutorials. it wasnt until I came across this website and found the code stop();
    first_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
    function mouseDownHandler1(event:MouseEvent):void {
    gotoAndStop(5);
    }

    second_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
    function mouseDownHandler2(event:MouseEvent):void {
    gotoAndStop(10);
    }

    that I’d been searching for like what seems now forever… and viola…you had the correct code to make my buttons work… THANKYOU!

  14. Hi all,
    I am creating a site for a not-for-profit org and have a navigation on the top that I want each button to link to a different page…
    here is my code
    stop();
    homebtn.addEventListener(MouseEvent.MOUSE_DOWN, mousedownHandler);
    function homebtnHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.islandcoastprimarycare.org”), “_self”);
    }
    aboutbtn.addEventListener(MouseEvent.MOUSE_DOWN, mousedownHandler2);
    function aboutbtnHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.islandcoastprimarcare.org/about.html”), “_self”);
    }
    resourcebtn.addEventListener(MouseEvent.MOUSE_DOWN, mousedownHandler3);
    function resourcebtnHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.islandcoastprimarycare.org/resources.html”), “_self”);
    }
    contactbtn.addEventListener(MouseEvent.MOUSE_DOWN, mouseddownHandler4);
    function aboutbtnHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.islandcoastprimarcare.org/contact.html”), “_self”);
    }

    I get the following errors
    ‘{‘ expected for function homebtnHandler(event:MouseEvent):void {

    and

    Unexpected ‘}’ encountered

    Can anyone tell me what is wrong with the code?
    Please and Thank you!! :)
    Shannon

  15. hello
    i am new to flash.

    i created a movie
    http://candmphotography.com/images/homebanner.swf

    now, i am trying to make it so when i click on each image, it goes to different url. for ex., if i click on baby photo, it goes to a url and if i click when a wedding dress shows, and i click on it, it goes to a different url.

    i added this action in an action layer, but when i publish it, i get an error msg.

    “Access of undefined property Kids.
    Access of undefined property Wedding.”

    as3:

    Kids.addEventListener(MouseEvent.MOUSE_DOWN, buttonClickKids);
    function buttonClickKids(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://candmphotography.com/children.html”));
    }

    Wedding.addEventListener(MouseEvent.MOUSE_DOWN, buttonClickWedding);
    function buttonClickWedding(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://candmphotography.com/wedding.htm”));
    }

  16. sorry, i got it to work after i posted this…
    thanks!

  17. Can you help me determine the code for your example that will make an email send? Actually more than one email button in one framde.

    I have been in flash for years, and after 8 hours of searching and trial and error, I am amazed how complicated something that used to be so simple has become.

    Thanks for your help.

    Rob

  18. ok… i’m working on my first Flash Website, and i’m having a little trouble. I have five buttons, and the all have different names, but i keep getting an error 1023, followed by a 1021 for the last 3 buttons the first two work great… help me please… here is my code:

    aboutus_btn.addEventListener(MouseEvent.MOUSE_DOWN,aboutus);
    function aboutus(event:MouseEvent):void {
    navigateToURL(new URLRequest(“Nebraska.html”), “_self”);
    }
    Trotn_btn.addEventListener(MouseEvent.MOUSE_DOWN, Trotn);
    function Trotn(event:MouseEvent):void {
    navigateToURL(new URLRequest(“/****nebraska/Nation_Wide.html”), “_self”);
    }
    IU_btn.addEventListener(MouseEvent.MOUSE_DOWN,IU);
    function IU(event:MouseEvent):void {
    navigateToURL(new URLRequest(“/****nebraska/Industrial.html”), “_self”);
    }
    MU_btn.addEventListener(MouseEvent.MOUSE_DOWN, MU);
    function MU(event:MouseEvent):void {
    navigateToURL(new URLRequest(“/****nebraska/****.html”), “_self”);
    }
    Join_btn.addEventListener(MouseEvent.MOUSE_DOWN, Join);
    function Join(event:MouseEvent):void {
    navigateToURL(new URLRequest(“/****nebraska/Membership.html”), “_self”);
    }

    Forgive the asterisks… it to protect my non-profit organization…
    please help i am supposed to have this site up 2-16-2010 at 9am

  19. kindly disregard… i found my answer… btw your site was very helpfull!!!

  20. Can you help me make the change to AS3 for this code:
    on (release) {
    getURL(clickTAG,’_blank’);
    }

    My ad servers require the clickTAG in the script but I can’t figure out how to make it work. The clickTAG is used by DART and Yahoo!’s ad server to locate the url for the clickthrough. We can’t hardcode urls.

    Thank you so much in advance!

  21. Dude your the best! awesome tutorial searched for hours for an answer for how to do this until i found ur post

  22. i have 3 button , in that main button inside another 3button ,inside button how to write a codu

  23. You’re a lifesaver, THANK YOU! :) Worked perfectly! Easy tutorial, much appreciated!

  24. Hi can you help me. Im using Flash cs3 actionscript 3.0.
    Ive created two scenes. In my 2nd scene I have a button that when clicked links to a game.
    Heres the code:

    beer_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(76);
    }

    In scene one I have another button. I want that button to take me to scene to: Heres the code for the button:

    stop();button1.addEventListener(MouseEvent.MOUSE_UP, mouseDownHandler);function mouseDownHandler(event:MouseEvent):void {

    gotoAndStop(1, “Scene 2″);

    }

    When I click on test movie this comes up:

    1021: Duplicate function definition.

    Im really stuck on this plz help me x

  25. hi,

    Please i need your help if u have time to help me with action script 3.0 ..

    ” I have 3 different video (video1, video2, video3)”

    and ” i have 3 different buttons (btn1, btn2, btn3″

    I need to make each button play one vide..
    for example btn1 play video1 and btn2 play video2 …etc

    when i click btn1 it will play the vide1 and all the buttons will be hidden if the video playing when video finish all the buttons will be visible again.. also when i click btn2 it will play video2 and all the buttons will be hidden when the video finish playing it will be visible again and the same thing for btn3…

    Please help me in this, i need the code for that if it is possible….

    thanx in advance ..

  26. Thank you very much for your tutorial it was really helpful.

  27. Thanks !!!. You saved my life.

  28. Hi,
    I am new to flash. I want to add four buttons in my flash file ( Play, Rewind, Forward, Stop). Can any one please help me the step by step procedures on how to add these buttons with Actionscript 3.0 in Flash CS4.

  29. flash cs3 is a waste!

  30. hey any and everyone. First off, thanks for this post. It works for me… almost.

    I am attempting to create a flash movie that has a bunch of buttons that when clicked will direct the movie to a certain frame on the timeline. I’ve got everything set up and the movie waits for my command upon playing, however, only some of the buttons work and some don’t. They all appear and react based on my Up, Down, and Over imagery I set up, but the actions don’t necessarily work. Then on top of that, when i find one of the buttons that work, it takes me to that frame where some different buttons will work, not necessarily the same any of the frames prior (in other words some of the buttons that didn’t work, now do but some of the buttons that did work now DON’T!!!?) it’s driving me crazy, I’ve tried many different ways to do this and i think this code is the simplest but I can’t figure out why it doesn’t connect with some of the buttons and not others. Please help if you can. The code is as follows:

    stop();

    btn_picsTL.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsTM.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsTR.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsML.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsMM.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsMR.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsBL.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsBM.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsBR.addEventListener(MouseEvent.CLICK, mClickHandle01);

    function mClickHandle01(pEvent):void {
    if( pEvent.target == btn_picsTL ) { gotoAndStop(“2″);}
    if( pEvent.target == btn_picsTM ) { gotoAndStop(“3″);}
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“4″);}
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“5″);}
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“6″);}
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“7″);}
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“8″);}
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“9″);}
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“10″);}
    }

  31. I’m very sorry for the continuation but i’ve figured out a more specific problem to my post directly above.

    I realize that when i click on a button that targets a frame that is after the current frame, it will advance. However, the buttons that link to a frame located prior to the current frame it will not go back. I can only continue the movie through frames that advance the timeline. How do i “gotoAndStop” to a frame that is before the current one on the timeline.

    THis is the current code:

    stop();

    btn_picsTL.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsTM.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsTR.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsML.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsMM.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsMR.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsBL.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsBM.addEventListener(MouseEvent.CLICK, mClickHandle01);
    btn_picsBR.addEventListener(MouseEvent.CLICK, mClickHandle01);

    function mClickHandle01(pEvent):void {
    if( pEvent.target == btn_picsTL ) { gotoAndStop(“2″); }
    if( pEvent.target == btn_picsTM ) { gotoAndStop(“3″); }
    if( pEvent.target == btn_picsTR ) { gotoAndStop(“4″); }
    if( pEvent.target == btn_picsML ) { gotoAndStop(“5″); }
    if( pEvent.target == btn_picsMM ) { gotoAndStop(“6″); }
    if( pEvent.target == btn_picsMR ) { gotoAndStop(“7″); }
    if( pEvent.target == btn_picsBL ) { gotoAndStop(“8″); }
    if( pEvent.target == btn_picsBM ) { gotoAndStop(“9″); }
    if( pEvent.target == btn_picsBR ) { gotoAndStop(“10″); }
    }

    Thanks for any help you can afford.

  32. I wanted to know if we have to use many buttons not just only two, to gotoAndPlay( some frame number).
    I want to create a function where I can just create one function. While running it I want to give it only the name of the button and the frame number, like we can easily do in case movie clips.

Leave a Reply