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:
- For basic information on creating a button in ActionScript 3.0, see this post: http://flashthusiast.com/2008/02/25/making-a-button-work-in-flash-cs3-with-actionscript-30-its-not-too-bad/
- If you have MORE THAN ONE BUTTON and want to add that to your FLA file, please continue on to this post about adding multiple buttons to your file.
- For info on targeting different windows with your button code (such as the same window instead of a new window), see this post: http://flashthusiast.com/2009/07/24/controlling-the-window-a-button-click-opens-using-actionscript-30-in-flash/
- For information on creating MovieClip buttons in Flash, see this: http://flashthusiast.com/2008/10/19/movie-clip-buttons-in-a-fight-its-as2-vs-as3-again/
- For information on controlling the browser window target that opens when a button is clicked, see: http://flashthusiast.com/2008/10/19/movie-clip-buttons-in-a-fight-its-as2-vs-as3-again/
- For info on creating buttons that go to the next frame or previous frame on the timeline, see: http://flashthusiast.com/2010/05/05/buttons-in-flash-using-actionscript-3-0-going-to-next-frame-and-previous-frame/
September 10, 2009 at 11:50 am
Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.
September 26, 2009 at 9:55 pm
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
September 26, 2009 at 9:56 pm
Sorry, can email the fla file if required
September 27, 2009 at 5:55 pm
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
September 28, 2009 at 3:55 am
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.??
September 29, 2009 at 10:03 am
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!
November 1, 2009 at 5:55 pm
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.
November 5, 2009 at 1:30 pm
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()
November 24, 2009 at 8:12 pm
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
November 26, 2009 at 4:49 am
from Malaysia with love!
thnks!
December 8, 2009 at 10:49 pm
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.
December 11, 2009 at 5:06 pm
You are awesome, the way you explain and demo is clear. Thank you for your blog and directions, now my homework is done.
December 19, 2009 at 5:33 am
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
December 19, 2009 at 1:21 pm
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.
December 21, 2009 at 3:55 pm
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.
March 17, 2010 at 10:11 am
thank you so much!
April 27, 2010 at 10:51 pm
Can anyone tell me how to link up the files on flash especially it only contains one scene whenever i open another file???
May 10, 2010 at 12:10 pm
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 ..
June 3, 2010 at 8:20 pm
I have been looking looking around for this kind of information. Will you post some more in future?
June 14, 2010 at 7:49 am
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.
June 20, 2010 at 2:02 am
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
June 20, 2010 at 2:04 am
how to flv file or other vedio media files embed in flash stage….plz snd me complete solution
June 29, 2010 at 1:04 am
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?
July 25, 2010 at 3:35 am
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.
July 28, 2010 at 4:57 pm
hello, ive been trying to link my scenes using your code.. but it was always error.. wah!! help me please!!! i am desperate!
August 2, 2010 at 7:36 am
Thank you, worked for me :)