Last Updated on February 26, 2019
Some people have been asking how to create button code in Flash (CS3, CS4, or CS5) that progresses the playhead in a SWF to the next or previous frame using ActionScript 3.0. This is not too difficult, and is a super simple code modification from any other button in AS3 (and is actually a touch easier than AS2 due to scoping issues).
This post includes an example of how to create two buttons that click between a few different frames. I’ll also include a (CS4) FLA file for you to look at and the AS2 equivalent.
The following code is for two buttons with the instance names button1 and button2. It uses the usual code for multiple buttons in a FLA file (for more information, see this post). A link to an example FLA follows the AS3 code.
stop();
button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void
{
prevFrame();
}
button2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
function mouseDownHandler2(event:MouseEvent):void
{
nextFrame();
}
Download the FLA file
In ActionScript 2.0, you need to do a bit of targeting to make things work correctly (although the button code is a bit simpler of course). Here’s the same thing in AS2:
stop();
button1.onRelease = function() {
this._parent.prevFrame();
};
button2.onRelease = function() {
this._parent.nextFrame();
};
(You could use _root instead of this._parent, although it isn’t a recommended practice).
MORE INFORMATION ON BUTTONS:
- 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 scenes or frames within a single scene, see this post here: http://foundpixel.com/articles/creating-buttons-that-link-to-different-scenes-using-actionscript-30/
- For info on targeting different windows with your button code (such as the same window instead of a new window), see this post:http://foundpixel.com/articles/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/
Vanessa says
thank you soooo much!!!!
Trevor says
Very concise clear explanation – Thanks for sharing! I’ve been avoiding AS3 for along time, but you explained this beautifully. Thanks again.
Caleb says
Very easy to understand i hated action script 3 because it wouldnt let me use things on buttons and stuff but now i under stand how it works thank you
Patrick says
Brilliant! The best conclusion to three hours of otherwise fruitless Googlin’
Thanks
Jonny Sooter says
How do I stop it from going on to the next scene?
I have this button but I want it to go to the next frame and when it hits the last frame in the scene for it to stop, and not continue on to the next scene
CandaceID says
Even with your code I get the following error again
“cannot access a property or method of a null object reference.”
Presumably this is because I am trying to do similar functions with similar button on another frame. i’ve changed the instance name, used that in the script, and changed the function name. What am I missing?
Tyler says
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
flash-lovers says
great tutorial. I have got the idea.thanks very much.
jeshika says
thanks for this.
Seba says
I like to say that the blog is excellent!
I’m trying to bluid a really easy file, with 5 buttons that change between 5 diferent images. No need for animation.
I searched your blog and find lots of things about buttons, but it keeps working the wrong way.
Any help you can give me?
Thaks!
Adrienne says
Hello Im having problems, my 1st frame code is
stop();
import flash.events.MouseEvent;
sync_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick (e:MouseEvent):void{
gotoAndStop(2);
}
This seems to work fine, and get my to the 2nd frame, which is where i have problems, my aim is to make a button to continue, and a button to go back to the first frame i p much copied what you used.
stop();
SyncMusic_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void {
gotoAndStop(3);
}
BktoConfig_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
function mouseDownHandler2(event:MouseEvent):void {
gotoAndStop(1);
}
The instance names were copied straight from where i named them so they are right, this is making me mad because ive been working on cs5.5 at uni, and on cs4 at home and my cs4 doesnt run my cs5.5 work i had made a start on (where all this was working)
I keep getting the error :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_fla::MainTimeline/frame2()
at flash.display::MovieClip/gotoAndStop()
at Untitled_fla::MainTimeline/onClick()
And the button going back doesnt work, Im in urgent need of help because this assignment is due next week, and ive been working on it for at least 2 weeks now and nothing i can do seems to be working.
Ernest Smith says
i tried your code.why wont my buttons go to the next frame.an another problem my test wont stop
owen says
there is a better script i use.
Marybeth says
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.
Ernie says
to get it to stop from going on and on simply put the code” stop () ; ” at the very beginning of your code
Bram says
Thankyou verry verry much!
Kim says
I am trying to make a link that goes to an internal page /frame. thismis the code that I am using:
stop();
but_my_27.addEventListener(MouseEvent.MOUSE_DOWN, myBtnHandler1);
function myBtnHandler1(event:MouseEvent) {void }
gotoAndStop(“25, Scene 1″);
}
the only thing I didn’t do was change the myBtnHandler to my button name. should I have done that.
Angela says
Thank you so so much. After 24 hours of finding the correct code I finally got it thanks to this posting!
Angela says
Thank you so so much. After 24 hours of SEARCHING FOR the correct code I finally got it thanks to this posting! (oops)
saifulislam says
Hi everybody
Would you please give me a solution about merge two .fla files in one master file.
That mean’s when I click on previous button the play .fla1 and when I click on Next button that play .fla2