Buttons in Flash using ActionScript 3.0: Going to next frame and previous frame

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).

Download the FLA here

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 2.0, ActionScript 3.0, buttons, FLA files, Flash CS3, Flash CS4, Flash CS5, Migration
  • Vanessa

    thank you soooo much!!!!

  • http://www.lunarstation5.com Trevor

    Very concise clear explanation – Thanks for sharing! I’ve been avoiding AS3 for along time, but you explained this beautifully. Thanks again.

  • Caleb

    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

  • http://www.ieltstestonline.com Patrick

    Brilliant! The best conclusion to three hours of otherwise fruitless Googlin’

    Thanks

  • Jonny Sooter

    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

    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

    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

  • http://www.flash-lovers.blogspot.com flash-lovers

    great tutorial. I have got the idea.thanks very much.

  • jeshika

    thanks for this.

  • Seba

    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

    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.

  • http://www.just4uweddingservices.com/Photography_1 Ernest Smith

    i tried your code.why wont my buttons go to the next frame.an another problem my test wont stop

  • http://0 owen

    there is a better script i use.

  • 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.

  • Ernie

    to get it to stop from going on and on simply put the code” stop () ; ” at the very beginning of your code

  • Bram

    Thankyou verry verry much!

  • Kim

    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.

  • http://www.agjidesigns.info Angela

    Thank you so so much. After 24 hours of finding the correct code I finally got it thanks to this posting!

  • http://www.agjidesigns.info Angela

    Thank you so so much. After 24 hours of SEARCHING FOR the correct code I finally got it thanks to this posting! (oops)