…and especially enthusiastic about motion tweens in Flash…
A common request in the comments is for information on how to create a button that links to a scene using ActionScript 3.0. 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.
If you’re unsure how to put multiple buttons in a document, see this post here: Putting multiple buttons in an FLA file.
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.
This blog specializes in the new motion model of Flash CS4, and is brought to you by team tween of Flash at Adobe. The blog will includes tips, techniques, and info on tweening in Flash using the Timeline and Motion Editor (as well as some stuff about motion runtime with code). From time to time, we might also talk about ActionScript 3.0. So if you're interested in tweens, this blog is for you. We also want to hear your feedback in the comments. All views expressed here are those of the authors, and do not necessarily represent the opinion or views of Adobe Systems Inc.

JK
August 4th, 2008 at 12:54 pm
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
August 5th, 2008 at 11:51 am
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!!
Chetan Sachdev
August 19th, 2008 at 1:38 am
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
August 27th, 2008 at 1:57 am
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.
September 2nd, 2008 at 3:02 pm
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.
jen d
September 2nd, 2008 at 9:39 pm
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.
Ken
September 9th, 2008 at 6:27 pm
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
September 14th, 2008 at 4:32 am
Hi,
I want to create button in flash…………. for that : If any user want to download a swf portfolio by button click.
honkuan
September 23rd, 2008 at 2:55 am
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
September 26th, 2008 at 2:02 pm
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
October 2nd, 2008 at 2:00 pm
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
October 2nd, 2008 at 2:09 pm
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
October 6th, 2008 at 1:57 pm
how do u rename the scene?
tomripl
October 6th, 2008 at 2:04 pm
it’s okay. figured this out already.
aiman
October 20th, 2008 at 5:47 am
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
October 24th, 2008 at 6:59 am
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
November 5th, 2008 at 9:11 pm
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
November 21st, 2008 at 7:51 am
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!