Tutorial: Pagination in ActionScript 3
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
for(i=0;i<1000;i++){
//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:
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
for(i=(page-1)*limit;i 1){
//take away all of the objects so we can make new ones
removeValues();
page --;
createValues();
//then update the page text
updateString();
}
}
function nextPage(event:MouseEvent):void{
//checking if the current page isn't too high
if(page < totalPages){
removeValues();
page ++;
createValues();
updateString();
}
}
//first things first, update the string to be as it is
updateString();
//then we place the movieclips onto the stage
createValues();
//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.
[…] 5 – Pagination in ActionScript 3 […]
September 17th, 2008 at 8:21 pm
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?
cheers
October 10th, 2008 at 9:46 am
hi
thanks for the great tutorial. i wonder if you can help with arranging the items as a grid?
cheers
October 27th, 2008 at 4:48 am
Nice code but change the removeValues() function to this:
function removeValues():void{
var i=0;
while(getChildByName(‘value’+i)){
removeChild(getChildByName(‘value’+i));
i++
}
}
This way there will be no error when you click the previous button from the last page.
October 31st, 2008 at 8:50 am
[…] 5 – Pagination in ActionScript 3 […]
November 3rd, 2008 at 9:55 pm
I suspect it will not last. ,
October 22nd, 2009 at 3:17 am
Remarks, questions and responses from both leaders are often jeered or cheered loudly by their respective party backbenchers. ,
October 22nd, 2009 at 10:06 pm
Hi, this is nice.. can you help me the same output for xml content
December 21st, 2009 at 7:51 am
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?
January 1st, 2010 at 7:22 am