Creating buttons that link to different scenes, and within a scene, using ActionScript 3.0

A common request in the comments is for information on how to create a button that links to a scene using ActionScript 3.0 in Flash CS4 or CS3. Luckily, it’s largely the same as creating a normal button that links to whatever, and uses the same gotoAndPlay format as in ActionScript 2.0. So hopefully it’ll make sense once you see it.

So if you’re unsure on general button code, see this post here: Making a button work in AS3. More links about writing button code at the end of this post.

Now you’re ready for some scenes-with-buttons action. Here’s some code:

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

gotoAndStop(1, "Scene 2");

}

So that code assumes that you have a button with the instance name button1, and a scene in your document called Scene 2 in the Scenes panel. The number "1" in that code points to frame 1 of the scene.

So what if you want to, say, link to a particular frame? Let’s link to frame 5 of a scene called monkeyScene:

stop();button2.addEventListener(MouseEvent.MOUSE_DOWN, monkeyDownHandler);

function monkeyDownHandler(event:MouseEvent):void {

gotoAndStop(5, "monkeyScene");

}

Hope that answers the question. I’ve put an FLA file online that links each scene using buttons so you can see this in action.

Linking to buttons within a scene

There have also been questions about linking to frames within a scene. I’m not positive what the specific problem is, but I’ll take a stab at it (or at least show what seems to work for me.

Now, what causes many of the problems with Scenes in Flash (and why they are officially discouraged from use in Flash best practices – I never use ‘em unless I have next to no code in a FLA), is as follows. From what I understand, when you publish your SWF file, all of the scenes in a FLA are made into one big giant string of frames. Kind of like sticking all of your scenes into one big timeline (one big scene, essentially) and pushing it out as a SWF. Therefore, this can mess up your code. If you’re pointing to a frame 56… which one? To you it may be frame 56 of scene 3, but to Flash that may be frame 56 of the giant new timeline in the SWF that’s published.

So, it’s good to avoid scenes.

But I digress. So if you’re linking to buttons within the same scene, you could specify what scene you’re pointing to in each button. So if you’re in a scene called monkeyScene, you could make your button that’s on frame 1 point to frame 2 of monkeyScene as follows:

stop();

mbutton1.addEventListener(MouseEvent.MOUSE_DOWN, monkeyHandler1);

function monkeyHandler1(event:MouseEvent):void {

gotoAndStop(2, "monkeyScene");

}

I’ve uploaded another real ugly FLA file that links multiple buttons to frames within a single scene. Check out monkeyScene in the FLA file.

MORE INFORMATION ON BUTTONS:

Jen deHaan is a consultant specializing in instructional design, web design, WordPress, technical project management, and writing. She is based in the San Francisco Bay Area.

Tagged with: , , , , ,
Posted in ActionScript 3.0, buttons, Flash CS3, Flash CS4, Flash CS5, Migration
  • JK

    So in ActionScript 2.0 you had:

    stop();
    myBtn_btn.onRelease = function(){
        gotoAndPlay("myScene");
    }
    

    But now in 3.0, you have:

    stop();
    myBtn.addEventListener(MouseEvent.MOUSE_DOWN, myBtnHandler1);
    function myBtnHandler1(event:MouseEvent):void {
        gotoAndStop(25, "Scene 1");
    }
    

    Why is it more complicated to do the same thing?

  • Lisa

    Halalujah!

    Your ugly like fla worked beautifully. I added “back” buttons on each frame, set the properities for it on each frame, copied and pasted the code for the “forward” button and made the appropriate changes….and wallah!

    I wonder if not using a scene name since I’m only working in one scence was part of the problem….

    Thanks Jen!!

  • http://cksachdev.blogspot.com Chetan Sachdev

    I am unable to find Window component which is in Flash 8 in Flash CS3. Could you please suggest an alternative to Window Component in CS3.

  • andia

    hi Jen!!
    first i would like to thank you for your fantastic tutorial!!! you are great.
    then i wanted to ask.: you have explained how to creat several buttons, and you explained how to link two or more scene together. well i wanted to creat a home page, in e way that when i click in a word, in the same page another page opens. i created 2 scene and used the way you explained to move from one scene to the other but i am not sure if this is the best way to have a new window in the same page, or should i use another method?
    andia

  • D.

    I thought it was strange to say that using Scenes is discouraged…I have been using them a lot and they have been working fine.

  • http://www.flashthusiast.com jen d

    Hey D,
    Good question. Generally they’re OK for code-less long animations, however there are many cases where it may be best to avoid using them. Here’s the pertinent section from Flash best practices on scenes that may help clarify (http://www.adobe.com/devnet/flash/articles/flash8_bestpractices_print.html). I wrote it for Flash 8, but it’s still relevant.

    Using Scenes

    Using scenes is similar to using several SWF files together to create a larger presentation. Each scene has a timeline. When the playhead reaches the final frame of a scene, the playhead progresses to the next scene. When you publish a SWF file, the timeline of each scene combines into a single timeline in the SWF file. After the SWF file compiles, it behaves as if you created the FLA file using one scene. Because of this behavior, avoid using scenes for the following reasons:

    * Scenes can make documents confusing to edit, particularly in multiauthor environments. Anyone using the FLA document might have to search several scenes within a FLA file to locate code and assets. Consider loading content or using movie clips instead.
    * Scenes often result in large SWF files. Using scenes encourages you to place more content in a single FLA file and hence, larger documents to work with and larger SWF files.
    * Scenes force users to progressively download the entire SWF file, even if they do not plan or want to watch all of it. Your user progressively downloads the entire file, instead of loading the assets they actually want to see or use. If you avoid scenes, the user can control what content they download as they progress through your SWF file. This means that the user has more control over how much content they download, which is better for bandwidth management. One drawback is the requirement for managing a greater number of FLA documents.
    * Scenes combined with ActionScript might produce unexpected results. Because each scene timeline is compressed onto a single timeline, you might encounter errors involving your ActionScript and scenes, which typically requires extra, complicated debugging.

    There are some situations where few of these disadvantages apply, such as when you create lengthy animations. If you create lengthy animations, you might find it advantageous to use scenes. If disadvantages apply to your document, consider using multiple FLA files, movie clips, or screens to build an animation instead of using scenes.

  • http://www.incomehorizon.com/media/gnome1/gnome.html Ken

    Learning Flash CS3 and Dreamweaver CS3 and having issues with the listing of the Gnome project you have listed. For some reason I have what I feel followed all the required parts and still my flash file doesn’t load in the browser (Firefox 3.0.1). Am I missing something?

    Operating System Vista Ultimate. Authoring software Adobe Design Premium CS3

    Any help would be appreciated (tested IE 7 just now and that too doesn’t display my gnome. : (

  • vijay

    Hi,

    I want to create button in flash…………. for that : If any user want to download a swf portfolio by button click.

  • honkuan

    he i have a problem here, when i apply the code into my button layer it having error code:

    1120: Access of undefined property button1.

  • Sarah

    Hi! I am doing a simple actionscript 3.0 assignment, however, it’s not so simple because I used scenes! Here’s the issue: my buttons aren’t going to the next scene when I click on them, and from what I can tell (as a newbie) I am using correct syntax etc. What is wrong? Here’s what I have:

    sp_button_1.addEventListener(MouseEvent.CLICK, Spring1);
    
    function Spring1(event:MouseEvent):void {
        gotoAndPlay(1, "Spring");
    }
    
  • Vic

    hello, i am having real troubles with this too. i am making a website and am using buttons to jump from scene to scene. when i put all the code in i keep on getting the same error message that it can not find the scene i am asking to jump to. do you have any suggestions what i might be doing wrong please? thank you. here is the error message.

    ArgumentError: Error #2108: Scene Animations was not found.
    at flash.display::MovieClip/gotoAndStop()
    at Website_1_Homepage_fla::MainTimeline/mouseDownHandler()

  • Vic

    i have just copy and pasted your button from the sample fla above, and now it works. i have been using the very basic buttons ‘up, over, down, hit’ is there a problem with doing this? thank you

  • tomripl

    how do u rename the scene?

  • tomripl

    it’s okay. figured this out already.

  • aiman

    hey jen. i’m using flash 8 and i have problems linking button/moviesclip within multiple symbols. i need to link it to another scene. it only works if the buttons is not within any symbols. if i put the buttons outside any symbols, other function wont work. currently the actionscript i’m using for my buttons that works outside any symbols but wont work within symbols is:

    _root.button1.onRelease = function(){
        gotoAndPlay("scene1", 1);
    }
    

    thank you.

  • rademenes

    Hi Jen!
    I’ve got problem with attached video into button.
    When I push the button ,video does’t stop and duplicate play every time when I repeat action .Have you any ideas how to solve this problem? pleasee :-( (Im newbie flash coder)

    Here is a code:

    stop();
    
    home_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
        gotoAndStop(1);
    }
    
    aboutyou_btn.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler2);
    function mouseDownHandler2(event:MouseEvent):void {
        gotoAndStop(10);
    }
    stop();
    
    message_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
    function mouseDownHandler3(event:MouseEvent):void {
        gotoAndStop(20);
    }
    							 
    media_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler4);
    function mouseDownHandler4(event:MouseEvent):void {
        gotoAndStop(30);
    }
    links_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler6);
    function mouseDownHandler6(event:MouseEvent):void {
        gotoAndStop(40);
    }
    
    stop();
    myspace_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler7);
    function mouseDownHandler7(event:MouseEvent):void {
        navigateToURL(new URLRequest("http://www.myspace.com/greg1775/"));
    }
    
    shred_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler8);
    function mouseDownHandler8(event:MouseEvent):void {
        navigateToURL(new URLRequest("http://www.shred-metal.org/"));
        gotoAndStop(1);
    }
    
  • Ben Levy

    I am using this code
    stop();
    more_btn.addEventListener(MouseEvent.MOUSE_DOWN, more_btnHandler1);
    function more_btnHandler1(event:MouseEvent):void {
    gotoAndStop(114);
    }

    When i click the button i made to go forward it goes forward and then when i click a back button i made it goes back to the beginning of the swf starting with my intro animation but the content the showed up when i clicked my back buttons is on top of every thing. who do i get it to just go back and not start all over again

  • jimmy

    halo there….
    i want to ask about: how to make a button(action script 3.0) to load another swf file? (not for website, just for interactive cd) plzz help me…..i am already very2 confuseddd…….

    Many thx!

  • Joe

    hello, please can you help me, i need it for uni work lol

    the issue is that i made a button to go forward on frame 1 to frame 10, the code being:

    stop();
    door1.addEventListener(MouseEvent.MOUSE_DOWN, secondDownHandler);
    function secondDownHandler(event:MouseEvent):void {
        gotoAndStop(10, "secondScene");
     }
    

    that works fine, but i want to place a button on the frame 10 to go baack to frame 1, but everytime i place the code in, it just plays my first scene and carrys on, thru the second scene, ignoring the stop code, this only happens if the code on the 10th frame is there, with it its fine.

    so i can you please tell me what im doing wrong, or is there a specific “back button”?

    cheers

  • Hadi

    Im a bit of a noob when it comes to action script, and i have been tring to get this working for ages but i can’t hack it.
    could you please give me sum guidence.

    how do i make a button(action script 3.0) to load another swf file.
    please help.. thx

  • slitherer

    Hello, good answers everyone! I would like to ask you something in the same area.. I now have a site.swf obj in a index.html page, what I would like to do is : to open by pressing a button another index1.html page which has the same site.swf obj embeded but in another scene.

    I would appreciate your help.
    By the way I am talking about AS2

    Thank you

  • nw

    hi there! as a total newbe to as3 is there a way to put ‘mouseover’ comments on to button instances?
    I have many buttons that are the same instead of having a seperate symble for each- goto page 1, home, top, etc

    thankyou

  • heymelby

    I’m totally frustrated, I have 3 embeded FLV’s and a 4th that is a segway with buttons on it. I want the SWF to play movie 1, then go to movie 4(with buttons) so users can click to see either video 1 again, 2 or 3. i’ve tried doing it with scenes but i’m to dumb to wrap my head around AS3. anybody got a solution.

  • arcueid

    hi~ I hope you are still alive… what if I want to load an external .swf file? what code should I use? …thanks~!^^-

  • http://www.bellarougedesign.com Terra

    I am kindof new to flash so please forgive me for asking a newbie question. I am a student taking graphic design and in our flash class our assignment is to create a fully functional portfolio website.
    I have done the design and created the pages as scenes within a scenes. (8 scenes. scene 1 is preloader and scenes 3-8 are duplicates of scene 2) Anyway, I cannot get the buttons to go to the pages when I test the movie.
    I am pasting the code I used for the buttons/pages (and this code is duplicated on every scene) currently I am getting 51 duplicate function definition errors when I test the movie and it just runs through all the scenes in order once and then stops.

    Code:
    stop();
    WSNB_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “Scene 2″);
    }

    stop();
    TOUR_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “Scene 3″);
    }

    stop();
    ABOUT_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “Scene 4″);
    }

    stop();
    GALLERY_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “Scene 5″);
    }

    stop();
    MERCH_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “Scene 6″);
    }

    stop();
    CONTACTBAND_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “Scene 7″);
    }

    stop();
    LINKS_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “Scene 8″);
    }

    Thank you for any help or suggestions!!!

  • Michel

    This really helps, but I have a movieclip on the stage that won’t stop playing. I want a button on ROLL_OVER to target that movieclip & then ROLL_OUT to move to another label within the clip. How is this achieved?

  • Andy

    Thanks heaps, great help! Was wondering if there’s a way to have transitions when using the script to change between frames in the same scene, rather than just jumping straight to that frame?

  • Polly

    AHH!! I LOVE YOU I LOVE YOU! YOU TOTALLY JUST HELPED ME SOLVE MY PROBLEM! THANK YOU SO VERY VERY MUCH! XD YES! AH-HA! I COULD NOT FIND ANYTHING LIKE THIS ANYWHERE! TT_TT So happy…

  • Justin

    This is causing my brain to bleed – I’ve copied and pasted the code above to get a button to link into a scene, but it doesn’t work. I’ve done it all exactly as demonstrated above, but when exporting to the swf file it seems it ignores the actionscript. Basically all my scenes just play (and loop) one after the other, not allowing me to even click the button. I got it to work once, but that was it – must have been a fluke.

    I’m not a flash programmer, but i need this for Uni too, and it’s driving me mad! esp since I’m on a deadline.

    Pls HELP!!

  • Justin

    Oh yeah – i downloaded the sample FLA file that links buttons to scenes, and it does exactly the same thing. Is there a problem with my compiler or something?

  • Annie

    PLEASE PLEASE can someone help me? I am trying to make a back button and I have put the action script on like so:

    stop()
    buttonback.addEventListener(MouseEvent.MOUSE_DOWN, goback1);
    function goback1(event:MouseEvent):void {
    gotoAndStop(1, “Scene 1″);
    }

    and I have put this code on the 2nd frame.
    And this is what i have put on my forward button:

    buttonforward.addEventListener(MouseEvent.MOUSE_DOWN,goforward);
    function goforward(event:MouseEvent):void {}
    gotoAndStop(2, “Scene 1″);

    It works clicking forward and back, and then forward and forward and back, but it will not let me go back twice. I have no idea why this happens when the code works the first time going from the second frame to the first, but will not work again once I have gone past the third frame.

    I would really appreciate any help or ideas people can give me,

    Thank you!

  • Brenda

    Hi, all. I’m actually getting back into Flash after being out of it for 7 years. (The last version I used was the first MX version, before Adobe bought out Macromedia.) I’m working with AS 3 in Flash Professional CS4, and I copied and pasted the code above to link my button to the next scene, but when I tested the scene, it gave me the following 3 syntax errors:

    Scene=Scene 2. layer=ABOUT. frame+1, Line 1 Statement must appear within the handler Source: stop();About_btn.addEventListener (MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownnHandler(event:MouseEvent):void {

    Scene=Scene 2, layer=ABOUT, frame=1, Line 3 Scene name must be quoted string Source: gotoAndStop(1, “Scene 3″);

    Scene=Scene 2, layer=ABOUT, frame=1, Line 1 Scene must appear within a handler Source: stop();About_btn.addEventListener (MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownnHandler(event:MouseEvent):void {

    Because I haven’t worked in Flash for so long, I’m going to assume operator error on my part, but any advice you can give me on how to resolve this will be greatly appreciated.

  • Annie

    Hi!
    Thanks so much for this tutorial! I would’ve been at it for hours trying to figure it out. I’m not very Flash savvy. Thanks for your help! (:
    - ANNIE

  • Misty

    I don’t get it. I have a scene, scene 1 that only has 1 frame in which I have a single stop code and have loaded all of my movie clips into it in different layers.. My buttons are part of a movie clip and I have been added to that scene as well. Now I want to link one of the buttons to a new scene upon hit. I can’t figure out how to do this. I don’t know where to put the above code. I’ve tried multiple solutions but it just plays frame 1 of scene one which is a background and then frame one of scene two which is nothing at this point so now Its just blinking at me. Can someone please help me?

  • Ximena

    Hi Misty I has the same problem, and I work like you: in a single frame, and movieClips distributed in layers. The only solution I have found is that the code of the button that you need to hit, is on the main line, I have it with a movie clip:

    mymovieclip_mc.visible = false;

    mymovieclip_mc.addEventListener(MouseEvent.CLICK,jumptoscene);

    function jumptoscene(event:MouseEvent):void
    {
    gotoAndPlay(1, “mynewscene”);

    }

  • Ximena

    sorry, please don’t worry about the first line, it’s about visibility.

  • Lani

    I downloaded the buttons-within-a-scene.fla I am using Flash CS4. When I preview the flash file, the screen shows a button with “label” as the text. In the output window, I get the following error.

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at buttons_fla::MainTimeline/__setProp_mbutton1_monkeyScene_buttons_0()
    at buttons_fla::MainTimeline/frame1()

    I find that Actionscript 3 is very frustrating. It used to be simple to create button navigation that called specific frames in the timeline.

  • http://www.flashthusiast.com Jen deHaan

    @Lani: I’m not running into any errors in Flash CS4 with the updater. Perhaps check that it is installed? (Help > Updaters). I know there were some issues fixed in this area.

  • http://www.flashthusiast.com Jen deHaan

    @Misty: Check out the attached FLA file to this blog post to see if it helps you in regards to where the code is placed and how the document in general is structured.

  • http://www.engsoc.org/~agutier2/ Ag

    Hi,

    I downloaded your fla example, and it didn’t work at first until I made the update. However I’m facing a huge problem when trying to do something extremely similar. On my website (http://www.engsoc.org/~agutier2/) I get an error, #1009 Cannot access property or method…..

    I have 22 buttons at scene 1, frame 1. Only one has Actionscript so far, this is linked to frame 2 (resume) so my problem is that when pressing the button to go back, it gives me that #1009 problem, as It goes back to the first frame but apparently the stage is not loaded or so I’ve read in so many more places.

    my code is the following:

    resume.addEventListener(MouseEvent.CLICK, clickSection);

    function clickSection(Event:MouseEvent):void
    {

    gotoAndStop(1, “Resume”);
    }

    and the button instance IS named “resume”

    and on the resume frame,

    stop();

    pdf.addEventListener(MouseEvent.CLICK, onMouseClick);
    function onMouseClick(event:MouseEvent):void
    {
    var targetURL:URLRequest = new URLRequest(“http://www.engsoc.net/~agutier2/images/Resume/AGutierrez.pdf”);
    navigateToURL(targetURL);
    }

    w2003.addEventListener(MouseEvent.CLICK, Clicking);
    function Clicking(event:MouseEvent):void
    {
    var targetURL:URLRequest = new URLRequest(“http://www.engsoc.net/~agutier2/images/Resume/AGutierrez.doc”);
    navigateToURL(targetURL);
    }
    w2007.addEventListener(MouseEvent.CLICK, Clicker);
    function Clicker(event:MouseEvent):void
    {
    var targetURL:URLRequest = new URLRequest(“http://www.engsoc.net/~agutier2/images/Resume/AGutierrez.docx”);
    navigateToURL(targetURL);
    }

    index.addEventListener(MouseEvent.CLICK, backtomain);

    function backtomain(event:MouseEvent):void
    {
    gotoAndStop(1, “Index”);
    }

    I would really appreciate if anyone could help me with this problem I’ve been on it for a few days now and I can’t find anyone having the same problem.

    Thank you guys for taking the time to help!

  • Jim

    Jen I have added a button like this

    hotAC_btn.addEventListener(MouseEvent.MOUSE_DOWN,
    hotACDemos_P_Handler);function
    hotACDemos_P_Handler(event:MouseEvent):void {

    gotoAndPlay(66);

    }

    frame 66 loads a child SWF … everything works great, but later in the movie after I remove that child my button does not work. So I put in

    hotAC_btn.addEventListener(MouseEvent.MOUSE_DOWN,
    hotACDemos_P_Handler);function
    hotACDemos_P_Handler(event:MouseEvent):void {

    gotoAndPlay(66);

    }

    again on another frame after the removeChild I get duplicate function defined. What code can i add to recall or reinitialize the button without having to add another one. I can get it all working if I add a the same button with different handler names, but that gets messy.

    Thanks in Advance

  • Jim

    Got it I think .. somehow flash was forgetting the call for the function. So I just added

    hotAC_btn.addEventListener(MouseEvent.MOUSE_DOWN,
    hotACDemos_P_Handler);

    back in on frame 65 and everything is working GREAT!

    If there is a better way I would love to know.

  • Ravi

    Hi All, I am new to AS3.0 please help me.
    I have four main buttons in a movieclip on the main timeline with AS telling them to gotoandplay different frames on the main time time line which has different movieclips at different frames. Inside the movie clips i have sub buttons which has gotoandplay script to different Frames in the same movieclip. it all plays well, but when i click on a button inside the movieclip to gotoandStop frame two it does and when i click on the main button nothing works. Can any one help me out , below is the code i have used.

    Main buttons code

    function main1Click(event:MouseEvent):void {
    MovieClip(root).gotoAndStop(“dashboard”);
    }
    mainBtn1.addEventListener(MouseEvent.CLICK, main1Click);

    function main2Click(event:MouseEvent):void {
    MovieClip(parent).gotoAndStop(“stats”);
    }
    mainBtn2.addEventListener(MouseEvent.CLICK, main2Click);

    function main3Click(event:MouseEvent):void {
    MovieClip(root).gotoAndStop(“four”);
    }
    mainBtn3.addEventListener(MouseEvent.CLICK, main3Click)

    sub buttons code indside moviclip

    stop();
    function testMain(event:MouseEvent):void {
    gotoAndStop(2);
    }
    test_btn.addEventListener(MouseEvent.CLICK, testMain);

    function Homebtn(event:MouseEvent):void {
    gotoAndStop(1);
    }
    home_btn.addEventListener(MouseEvent.CLICK, Homebtn);

  • aaron

    Sarah (posted on September 26th, 2008 at 2:02 pm),

    did you ever get your problem fixed? i am using almost identical code and am having the same issues. they buttons work in the first scene, past that, nothing works.

    thanks!

  • http://www.flashthusiast.com Jen deHaan

    @aaron: Make sure you have a stop() action on the other scene(s). My guess is your movie may be hitting the end of the timeline and going back to the beginning?

  • MiraS

    Hello,
    I’m new in Flash and I’m very thankful when I find places like this site. I’ve read the replies here, trying to figure out my problem but i couldn’t so I’m asking for some help.
    I’m making lets call it a presentation and have 1 scene for the intro, 2nd for Menu with 5 btns for now and the rest of the scenes are the “goals” of the buttons. Pretty simple right. I have no problems linking 1st and 2nd scenes but when I tried to link one of the buttons to its goal scene I got an error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Projectandscenes_fla::MainTimeline/frame316()

    I don’t have frame 316 in either of my scenes. It’s nothing there so I don’t know how to fix it.

    Please help and thank you for the time.

  • Jim

    Jen HI! Thanks for this great for this great POST. It was a huge help! I would like a button to go to a new URL load that movie and play a specific scene. Can that be done? If so how might I do that?

  • Jackie

    trying to figure this out: i have 2 scenes in my fla. one is the preloader, the other is scene 1. in scene 1 i have a layer named menu and a layer names contents. the menu layer has 7 buttons, the contents layer has a movie clip. within that movie clip, there are 2 layers, one with action script and the other with text. the text layer has 7 frames each with different text. what im trying to do is link the buttons on the main scene (scene 1) to the frames within the movie clip in the contents layer. how do i do that?

    if the frames were within the any layer on scene 1 it would be easy with the instructions above. but they are within a layer. help? been trying to figrue out for 3 days already (self teaching flash newbie here…)

  • http://www.flashthusiast.com Jen deHaan

    @MiraS: When a SWF is compiled, it puts all of the Scenes onto a single timeline. Say your first scene has 200 frames, frame 316 would be frame 116 of Scene 2. Hope that makes sense. The first thing I’d check with that error is whether your code and buttons are at the same frame location (for example, if your code was at frame 1 but your buttons at frame 2, you would see such an error.

    @Jim: You could use the button code to get a new URL from here: http://flashthusiast.com/2008/02/25/making-a-button-work-in-flash-cs3-with-actionscript-30-its-not-too-bad/. In the SWF that is on the second page, you could load the scene. If you’re wanting to target specific scenes from the different buttons, you might want to look into URL variables so you can target them from the button URLs and use that in the second SWF.

    @Jackie: To target a frame inside a MovieClip, you would use code such as the following (where myMC is your movie clip):

    button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownHandler(event:MouseEvent):void {
    myMC.gotoAndStop(5);
    }

    So the simplest revision would be to just add that one additional line (go to and stop at the scene, and go to and stop at the MC’s frame), as in the following:

    button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, “scene2″);
    myMC.gotoAndStop(5);
    }

  • http://webpost.isws.illinois.edu/ Kevin

    Hello. I have a Flash CS4 .fla that has four buttons in it – it is a single scene. Each button goes to a different frame in the file. The code is very straightforward and is similar to examples I have found all over this site. However, only the first three buttons work. The fourth button, if clicked on, will display the proper contents, but the other buttons will not take you back to the other scenes. To make this even more obtuse, if you let the file work its way down to the fourth button on its own (without clicking on button 4 to go there immediately) then the buttons DO work. I am all out of ideas. I have worked with Flash since version 5 and have not seen this kind of craziness before.

    I thought it might be a corrupt file, so I re-created the project again from scratch, and I get the same thing. Help?

    Here is the URL to view the file: http://webpost.isws.illinois.edu/

    And here is the code for the buttons under button 4:

    ButtonS4B1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandlerS4B1);
    function mouseDownHandlerS4B1(event:MouseEvent):void {
    gotoAndPlay(1, “Scene 1″);
    }

    ButtonS4B2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandlerS4B2);
    function mouseDownHandlerS4B2(event:MouseEvent):void {
    gotoAndPlay(81, “Scene 1″);
    }

    ButtonS4B3.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandlerS4B3);
    function mouseDownHandlerS4B3(event:MouseEvent):void {
    gotoAndPlay(161, “Scene 1″ );
    }

    I also have similar code (that works) for Button Three, etc:

    ButtonS3B1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandlerS3B1);
    function mouseDownHandlerS3B1(event:MouseEvent):void {
    gotoAndPlay(1);
    }

    ButtonS3B2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandlerS3B2);
    function mouseDownHandlerS3B2(event:MouseEvent):void {
    gotoAndPlay(81);
    }

    ButtonS3B4.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandlerS3B4);
    function mouseDownHandlerS3B4(event:MouseEvent):void {
    gotoAndPlay(241);
    }

    I am more than willing to provide the source .fla file if you are interested in taking a quick look.

    Thanks!

  • sandrar

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  • Bob

    Brand new to Flash. Need help with AS3, not for a website, but for student interactive learning

    I have 1 generic button that I use called next. I have 3 copies this button and given each copy a different instance name.

    Easy part first,
    1 button I want to do a frame by frame advance
    1 button I want to do a frame by frame previous

    Code I have as follows

    MasterPrev_btn.addEventListener(MouseEvent.MOUSE_DOWN, MasterPrevDownHandler);

    function MasterPrevDownHandle(event:MouseEvent):void
    {
    gotoAndStop(prevFrame);
    }

    MasterNext_btn.addEventListener(MouseEvent.MOUSE_DOWN, MasterNextDownHandler);

    function MasterNextDownHandler(event:MouseEvent):void
    {
    gotoAndStop(nextFrame);
    }

    The 3rd button is a little more difficult, maybe!!

    I want this to randomly jump between frame lables.

    Code for this I have tried

    MasterRand_btn.addEventListener(MouseEvent.MOUSE_DOWN, MasterRandDownHandler);

    function MasterNextDownHandler(event:MouseEvent):void
    {
    choice = Math.round(Math.random()*33);
    switch (choice)
    {

    I have got this working in AS2, however on another scene I have a Drag&Drop facility that I want to use

    Surprisingly enough this code doesn’t work, that why I need help :(

    help is very much needed

  • Bob

    Sorry, can email the fla file if required

  • Bob

    Ok I managed to figure out the frame by frame adavence and previous by using the code

    MasterNext_btn.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandlerMN);

    function mouseDownHandlerMN(event:MouseEvent):void {
    gotoAndStop(currentFrame+1);

    But help is required for the random. Please I would like to get this going for my Students soon.

    Thanks

  • Bob

    no worries, managed to fix that problem.

    Who’s a drag and drop expert??

    new problem arisen hehe
    multiple frames drag and drop, first frame drags and drops fine, however when move to next frame, the first image is still there, how to refresh the screen to prevent the previous image from appearing on the current frame.??

  • Josh

    Hey I’m getting the TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at DSWEBSITE2_ABOUT_fla::MainTimeline/frame26()

    I don’t really understand why… the buttons on the main page work fine…. but on the 2nd page, “About” page… none of the buttons work and I get the error once when the action script frame comes up….

    stop();home_btn.addEventListener(MouseEvent.MOUSE_DOWN,
    mouseDownHandler5);function
    mouseDownHandler5(event:MouseEvent):void {

    gotoAndStop(98,”HOME”);

    }

    projects_btn.addEventListener(MouseEvent.MOUSE_DOWN,
    mouseDownHandler6);function
    mouseDownHandler6(event:MouseEvent):void {

    gotoAndPlay(1,”PROJECTS”);

    }

    video_btn.addEventListener(MouseEvent.MOUSE_DOWN,
    mouseDownHandler7);function
    mouseDownHandler7(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.vimeo.com/6039176″));
    }

    contact_btn.addEventListener(MouseEvent.MOUSE_DOWN,
    mouseDownHandler8);function
    mouseDownHandler8(event:MouseEvent):void {

    gotoAndPlay(1,”CONTACTS”);

    }

    thats the code I’m using of the page that doesn’t work… each one is segmented into scenes… I don’t get it… ugh… must kill… or get helped…. please help!

  • Karbo13

    Just an observation:

    In your file to download (buttons-scenes.fla), I got a error, the buttom in frame 5 in monkeyScene has same instance name and same function name, then I change both names and put my own buttoms. Now all is OK.

    Thank you very much for this tutorial.

    A greeting from Guatemala, City.

  • josue

    ok iam working on cs4 with 3 frames iam using this
    stop();
    ONE.addEventListener(MouseEvent.CLICK, clickButtonHandler);
    function clickButtonHandler(event:MouseEvent):void {
    gotoAndStop(2);
    }
    TWO.addEventListener(MouseEvent.CLICK, clickButtonHandler2);
    function clickButtonHandler(event:MouseEvent):void {
    gotoAndStop(3);
    }

    for my buttons it goes to the next frame but then it doesn’t want to go to the third frame I GET
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at project6_Scene1_fla::MainTimeline/frame1()

  • mwa

    HELP ME . . so i have transfered my flash file from my skool comp to my home one, both use Flash CS4 and action script 3 . . BUT . . when i transfer it onto my home Flash program it comes up with heaps off errors! and on the skool computer it was fine!

    ALSO . . why cant it find my scene when i know its in the same file?
    eg:

    ArgumentError: Error #2108: Scene CoverPage was not found.
    at flash.display::MovieClip/gotoAndPlay()
    at CLICKTOVIEW_MapOfAus_fla::MainTimeline/enterBookPage()

    I really need help . .
    anything would be good right now!
    Thanks,
    mwa

  • JaymanJohn

    from Malaysia with love!

    thnks!

  • Pontus

    Hi, i have a menu in my fla file where i want to link to 4 diffrent scenes. I have used this code:
    stop();b60.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);function mouseDownHandler1(event:MouseEvent):void {

    gotoAndPlay(1, “Scen 1″);

    }
    b70.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);function mouseDownHandler2(event:MouseEvent):void {

    gotoAndPlay(1, “Scen 44″);

    }
    b80.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);function mouseDownHandler3(event:MouseEvent):void {

    gotoAndPlay(1, “Scen 45″);

    }
    b90.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler4);function mouseDownHandler4(event:MouseEvent):void {

    gotoAndPlay(1, “Scen 50″);

    }

    But it dosent work.
    The movieclip i have in the file dosent start when i add this code and if i click the buttons they say that the scenes that i have in my file dosent exist.
    How can i solve this?
    Please help.

  • meda

    You are awesome, the way you explain and demo is clear. Thank you for your blog and directions, now my homework is done.

  • jouky

    Hi. I’m having a problem with my flash website using AS3.
    Here is the problem I encountered, whenever I click a button on my website it just keep poping out this error message :

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at CFMWEBSITE_fla::MainTimeline/frame2()
    at flash.display::MovieClip/gotoAndStop()
    at CFMWEBSITE_fla::MainTimeline/profile_homepage()

    Here is my code:
    stop();
    homebutton.addEventListener(MouseEvent.MOUSE_DOWN,home_linkpage);
    function home_linkpage(event:MouseEvent):void {
    gotoAndStop(1,”Homepage”);
    }

    profilebutton.addEventListener(MouseEvent.MOUSE_DOWN, profile_linkpage);
    function profile_linkpage(event:MouseEvent):void {
    gotoAndStop(1,”Profilepage”);
    }

    animationbutton.addEventListener(MouseEvent.MOUSE_DOWN,animation_linkpage);
    function animation_linkpage(event:MouseEvent):void {
    gotoAndStop(1,”Animationpage”);
    }
    gallerybutton.addEventListener(MouseEvent.MOUSE_DOWN, gallery_linkpage);
    function gallery_linkpage(event:MouseEvent):void {
    gotoAndStop(1,”Gallerypage”);
    }
    linkbutton.addEventListener(MouseEvent.MOUSE_DOWN, link_linkpage);
    function link_linkpage(event:MouseEvent):void {
    gotoAndStop(1,”Linkspage”);
    }

    How can I solve this problem? Thank

  • Garth

    Hope you can help. I am new to flash, but am on a steep learning curve as am a fast study. I am building my first site and it is a bit complex. It resembles a family tree structure of movie clips within movie clips with buttons animating in other movie clips that are mixed into more movie clips. Seems a bit much perhaps, but it was needed to acheave the desiered look and functionality. Problems started with navigating a time line from buttons located tree or so branches down. Though I never found anything to help me in any tutorial I looked at, I did figger it out. The code needs to be in the timeline that you are navigating while you referance the button instance. Problem solved you may say, well no. A new one popped up that is to me not saposed to be there, which is that while navigating the buttons, the ones that scroll out for finer navigation do not always work. Hard to explane it better with out writing a book on it.

    Hope you can help or someone to give an idea.

  • Garth

    Update… Still no luck, even tried re-aranging everything to different locations. Seems that I am able to brouse the menu´s top down, but go bottom up and they no longer work. Home button re-sets the scene, so if I know where I want to go, then I can go straight there. No good though for people brousing the site though. Any thoughts on why. Would not have this problem if I made the site in Dreamweaver. P.S. I had started in Dreamweaver, but menus and frames do not mix well if the frame is not big enough.

  • http://prifreire.com pri freire

    thank you so much!

  • Darklord

    Can anyone tell me how to link up the files on flash especially it only contains one scene whenever i open another file???

  • allen

    hi,

    Please i need your help if u have enough 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 ..

  • mobilya modelleri

    I have been looking looking around for this kind of information. Will you post some more in future?

  • Resha_Resha

    Hi every body,
    Special Hello for Jen Dehaan,
    Well, I tried that piece of code to go to another scene and it didinot worked with me,
    I downloaded the .fla file and it worked very well,I tried to make another by I ALSO COULDNOT
    I need a simple way to go from scene to another on button CLICK very emergencely as Iam a beginner and I really neeeeeeeeed them badly.
    Thanks 4 all of you.

  • http://www.rmbtech.webatu.com rashid

    plz help me about this:
    i have flash file and i make a btn in home page and button name contact us let supose and i have make another page and page name is contac us
    and i want that then i clink on btn contact when page root goto on contactus page ,,,if u have complete code and template so email me ,,thankful dear

  • http://www.rmbtech.webatu.com rashid

    how to flv file or other vedio media files embed in flash stage….plz snd me complete solution

  • Jonathan Craft

    Hi Jen

    Thanks for the great tutorial, i have now linked my button in scene 1 to link to frame 1 in scene 2.

    One problem

    The animation in scene one now stops as the first line of the code is ()stop. I want this animation to carry on playing until the visitor clicks on the button. I removed the ()stop part of the the code and now it obviously plays through the scenes making the button redundant. Is it a case of importing a movie into a movie to allow the animation to play whilst waiting for interaction from the visitor?

    Any help would be great, i can send you my FLA if it that helps?

  • mike4

    Hello.
    I have a problem using AS.
    I create flash banner (with several companys and their logos), which are played. Now i would like, that when the company logo is shown wiht mouse click it will open their website. I’m working with scenes, so I have several scenes.
    When I test each scene seperatly it work fine, but if I would like to preview all, then it stops. It show: Duplicate function definition.
    I didn’t find solution for this problem, so I wold be happy if you any of you can help me with this.

  • angie

    hello, ive been trying to link my scenes using your code.. but it was always error.. wah!! help me please!!! i am desperate!

  • Matt

    Thank you, worked for me :)

  • http://www.asyacreative.com Asya

    Thanks for the explanation – took me a while to find it explained so simply!
    Only thing I would add is an example of redirecting to another frame in the same scene…in my instance, it was just:

    play_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    gotoAndPlay(2);
    }

  • http://www.indianvisual.co.uk raqutus

    Hi I’m new to Cs4 and As3 hope you can help me with this problem., it’s pretty much the same as most on this forum.

    I have a button within a movie let’s call it “enter_bnt” and the movie is “lostMovie”. The movie is imported into my main scene “intro” via a UILoader at the end of lostMovie on the last frame I have placed enter_btn.
    This enter_btn should direct me to the next frame in my main SWF but running in debugger I get the following:

    ArgumentError: Error #2108: Scene intro was not found.
    at flash.display::MovieClip/gotoAndPlay()
    at lostMovie/skipToIntro()
    Cannot display source code at this location.

    the button function is :
    enter_btn.addEventListener(MouseEvent.CLICK,skipToIntro);
    function skipToIntro (event:MouseEvent){
    MovieClip(root).gotoAndPlay(3,”intro”);

    trace(“button Pressed”);

    }

    Do I have to add any script to the intro.as or the lostMovie.as files.?
    Should I not use nested buttons.?
    Any help or direction will do as I’m stummped and going round in circles at the moment.
    Many thanks.

  • George

    Hello there, Thanks for publishing the info about buttons and frame labels. I have successfully established a button which jumps to a frame number. I have several buttons with different frames to jump to. When I add a second on my actions layer my entire timeline loops. I also lose a fade in image which I wanted to feature when my page loads. Can you advise on how I can stop the main timeline looping?
    Thanks

  • Darren

    Hi,

    Is it possible to use Radio Buttons for jumping to a frame, label or Scene?

    I am stuck trying to create a situation where the user clicks a Radio button, and when they click the next button, they go to a frame or Label or scene based on the Radio button clicked.

    I cannot find a solution to this, even with Adobe.

  • Tyler French

    i am using this code

    stop();
    forumslink.addEventListener(MouseEvent.MOUSE_DOWN, myBtnHandler1);
    function myBtnHandler1(event:MouseEvent):void {
    gotoAndStop(1, “Scene 3″);
    }

    with forumslink being a button which links to a forums scene, scene 1 homepage and scene 3 the actual forum

    although when i debug it the forum button doesnt work on the homepage
    do i need to edit the code more?
    as you can tell i am new to flash cs5

    any help appreciated :D

  • Grant

    Finally a tutorial that made this button crap make sense, kudos to you mate. thx

  • http://undersetting nrajoo

    how to sole this problem..my button action not working..

    **Error** Scene=scene 1, layer=background, frame=1:Line 1: Statement must appear within on handler
    stop();

  • http://rcanfield001.deviantart.com/gallery/ Rob C

    Thanks for the tutorial, it was VERY helpful. I have to say I am missing AS2, I understand that AS3 is better in the long run, but I am just not used to it yet.

  • Kyky25

    GAAAAHHHH!!!
    This is so annoying!!!
    I’m trying to make a flash website for school and I tried the code but it didn’t work.
    I just keep getting error about expecting rightparen before ___.
    I’m never useing scenes again

  • http://www.theparodynetwork.com Salvy

    Thank you much for the code. It was just what I was looking for. :)

  • Marybeth

    Is there a way to gotoAndPlay starting at a specific keyframe, but then have it stop at a specific keyframe instead of playing to the end of the scene?

    Thank you.

  • fabrice

    Hello Im newbie in as3, first of all thanks for all the topic this help me very much!

    I create multiple button and all this button link to another scene, this work well but i wanna use my button in every scene like a navigation toolbar, if i wanna do this i need to create xxx reference like mouseDownHandler9, mouseDownHandler656 etc..

    My actual code :
    stop();btn_LIGHTING.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);function mouseDownHandler1(event:MouseEvent):void {

    gotoAndStop(1,”LIGHTING”);
    }

    btn_FAC.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);function mouseDownHandler2(event:MouseEvent):void {

    gotoAndStop(1,”FAC”);

    }
    btn_IFE.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);function mouseDownHandler3(event:MouseEvent):void {

    gotoAndStop(1,”IFE”);

    }
    btn_AIR.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler4);function mouseDownHandler4(event:MouseEvent):void {

    gotoAndStop(1,”AIR MANAGEMENT”);

    }

    btn_GALLEY.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler5);function mouseDownHandler5(event:MouseEvent):void {

    gotoAndStop(1,”GALLEY”);

    }

    btn_ESCAPE.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler6);function mouseDownHandler6(event:MouseEvent):void {

    gotoAndStop(1,”ESCAPE”);

    }
    btn_PSU.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler7);function mouseDownHandler7(event:MouseEvent):void {

    gotoAndStop(1,”PSU”);

    }
    btn_SMOKE.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler8);function mouseDownHandler8(event:MouseEvent):void {

    gotoAndStop(1,”SMOKE”);

    }
    btn_logo.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler9);function mouseDownHandler9(event:MouseEvent):void {

    gotoAndStop(5);

    }

    How to do to use the button in different scene?

    thank