Tutorial: Pagination in ActionScript 3

Written By MrSun at 8:00 am - Thursday, August 21st, 2008
Categories: All Tutorials, AS3, Beginner Tutorials, Flash

This is going to be another tutorial about pagination. This time, however, it will be done in ActionScript 3.0. Here’s a brief description of what this tutorial will be about, taken from my previous tutorial in ActionScript 2.0:

This is going to be a smaller tutorial about how to paginate objects in ActionScript 2.0. By paginate, I mean taking a large group of objects, and making it so the user can navigate through them by page. For example, in our previous tutorial, Make a Rhythm Game in AS2, we made a list of levels that the user could play. However, this list was only limited to 5 levels, and we didn’t paginate them. We’re going to do something like this today.

In this tutorial, I’m just going to take excerpts of text from my AS2 tutorial and will make updates in the code a bit. The process will be the same, just with different code. Let’s get started, shall we?

First of all, we’re going to make a large array of random values that we can paginate. Because we don’t care what the values are right now, we’re just going to make them totally random. If you’re going to use pagination for something more than a tutorial, then you probably should use your own values.

//this values array will hold all of the values we need to paginate
var values:Array = new Array();
//defining the 'i' variable so we can use it multiple times in for loops
var i:int;
//this loop adds 1000 different values to the array
	//the values will range from 0-9999
	values[i] = Math.round(Math.random()*9999);

If you want to check, then try making a trace() statement to see what the values are.

Next, we’re going to have to make a MovieClip called “mcValueHolder” that will be placed onto stage with a certain value in the array within it. We also need a dynamic text field to actually have the value in it. Here’s what mine looks like:

The MovieClip

Also, give the text field an instance name of “txtValue”, so we can reference it in actionscript.

Now, we have to export the MovieClip for ActionScript. If you don’t know how to do that, then right click on the MovieClip in the library and click on “Linkage…”. Then, check off “Export for ActionScript”. That’s all you have to do. Click on “Ok” and we can continue.

We also need two buttons to click to get to the next page and the previous page. Hopefully, you can figure out how to do that yourself. Just add them to the bottom of the stage and give them instance names of “mcPrev” and “mcNext”.

Also, we need to make a text field which tells the user which page they are on out of the total pages. Just add it to the stage and set the instance name to txtPage.

Now for the hard part, actually coding the pagination. Place this code in after we define the values variable.

//the current page we're on
var page:int = 1;
//the limit of values we need per page
var limit:int = 6;
//the current row that we're working on
var row:int = 0;
//The total pages that we have
var totalPages:int = Math.ceil(values.length/limit);
function createValues():void{
	//this will always limit the amount to the limit variable
	//but, "i" will act as a marker for which part of the values
	//array that we're going to use
		//checks if there actually is a value where "i" is
		//otherwise we'll get some undefined movieclips
		if(i < values.length){
			var newValue:mcValueHolder = new mcValueHolder();
			//sets the coordinates based on the row
			newValue.x = 5;
			newValue.y = 5+newValue.height*row;
			//sets this guys value to the correct part of the array
			newValue.txtValue.text = values[i];
			//changing this guys name so we can reference it later
			newValue.name = 'value'+row;
			//adds the mc to stage
			//move onto the next row
			row ++;
	//then we reset the rows so we can use the variable again
//function to remove the mc's
function removeValues():void{
	//this loop will run once for each mc on stage
		//get the object in the current row and kill it!
//we're defining a function which updates the string
function updateString():void{
	txtPage.text = 'Page '+page+' out of '+totalPages;
//next page and previous page functions
function prevPage(event:MouseEvent):void{
	//checking if the current page isn't too low
	if(page > 1){
		//take away all of the objects so we can make new ones
		page --;
		//then update the page text
function nextPage(event:MouseEvent):void{
	//checking if the current page isn't too high
	if(page < totalPages){
		page ++;
//first things first, update the string to be as it is
//then we place the movieclips onto the stage
//adding listeners to the buttons
btnPrev.addEventListener(MouseEvent.CLICK, prevPage);
btnNext.addEventListener(MouseEvent.CLICK, nextPage);

Pretty intense, eh? Well, this code is basically useless by itself. I doubt that anybody wants to look through random numbers. But, implementing it into your project will help a lot.

The Final Product

Source .fla file



This is a very helpful article, thank you.
I have noticed a error that happens when you get to the last record/page then click back on the prev button the following Error occurs:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at pagination_fla::MainTimeline/removeValues()
at pagination_fla::MainTimeline/prevPage()

For now I cant work out why, do you know?


thanks for the great tutorial. i wonder if you can help with arranging the items as a grid?



Nice code but change the removeValues() function to this:

function removeValues():void{
var i=0;

This way there will be no error when you click the previous button from the last page.


I suspect it will not last. ,


Remarks, questions and responses from both leaders are often jeered or cheered loudly by their respective party backbenchers. ,


Hi, this is nice.. can you help me the same output for xml content


for some reason the very first bit of code gives me three errors

1084: Syntax error: expecting rightparen before semicolon.
1084: Syntax error: expecting semicolon before rightparen.
1084: Syntax error: expecting rightbrace before end of program.

does the code need to be updated?