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:

19 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

WP-SpamFree by Pole Position Marketing