{"id":545,"date":"2008-08-16T08:00:08","date_gmt":"2008-08-16T12:00:08","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=545"},"modified":"2022-05-29T08:23:36","modified_gmt":"2022-05-29T12:23:36","slug":"tutorial-pagination-in-actionscript-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-pagination-in-actionscript-2\/","title":{"rendered":"Tutorial: Pagination in ActionScript 2"},"content":{"rendered":"<p>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, <a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-make-a-rhythm-game-in-as2\/\">Make a Rhythm Game in AS2<\/a>, we made a list of levels that the user could play. However, this list was only limited to 5 levels, and we didn&#8217;t paginate them. We&#8217;re going to do something like this today.<\/p>\n<p>First of all, we&#8217;re going to make a large array of random values that we can paginate. Because we don&#8217;t care what the values are right now, we&#8217;re just going to make them totally random. If you&#8217;re going to use pagination for something more than a tutorial, then you probably should use your own values.<\/p>\n<p>Make a layer called &#8220;actions&#8221; and place the following code in the first frame:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/this values array will hold all of the values we need to paginate\r\nvar values:Array = new Array();\r\n\r\n\/\/this loop adds 1000 different values to the array\r\nfor(i=0;i<1000;i++){\r\n\t\/\/the values will range from 0-9999\r\n\tvalues[i] = random(9999);\r\n}\r\n<\/pre>\n<p>If you want to check, then try making a <tt>trace()<\/tt> statement to see what the values are.<\/p>\n<p>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 text field to actually have the value in it. Here's what mine looks like:<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/paginate-as2\/movieclip.png\" alt=\"The MovieClip\" \/><br \/>\nAlso, in the text field's \"Var:\" field, put in <tt>thisValue<\/tt>. This will be the value that the MovieClip holds.<\/p>\n<p>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.<\/p>\n<p>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\".<\/p>\n<p>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 \"Var: \" field to <tt>pageTxt<\/tt>.<\/p>\n<p>Now for the hard part, actually coding the pagination. Place this code in after we define the <tt>values<\/tt> variable.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/the current page we're on\r\nvar page:Number = 1;\r\n\/\/the limit of values we need per page\r\nvar limit:Number = 6;\r\n\/\/the current row that we're working on\r\nvar row:Number = 0;\r\n\/\/The total pages that we have\r\nvar totalPages:Number = Math.ceil(values.length\/limit);\r\n\/\/the text at the bottom of the page\r\nvar pageTxt:String = 'Page '+page+' out of '+totalPages;\r\n\r\n\/\/the function that will add all of the mc's to stage\r\nfunction createValues():Void{\r\n\t\/\/this will always limit the amount to the limit variable\r\n\t\/\/but, \"i\" will act as a marker for which part of the values\r\n\t\/\/array that we're going to use\r\n\tfor(i=(page-1)*limit;i<page*limit;i++){\r\n\t\t\/\/checks if there actually is a value where \"i\" is\r\n\t\t\/\/otherwise we'll get some undefined movieclips\r\n\t\tif(i < values.length){\r\n\t\t\t\/\/attaches the movieclip to the stage\r\n\t\t\tattachMovie('mcValueHolder', 'value'+row, getNextHighestDepth());\r\n\t\t\t\/\/sets the coordinates based on the row\r\n\t\t\t_root['value'+row]._x = 5;\r\n\t\t\t_root['value'+row]._y = 5+_root['value'+row]._height*row;\r\n\t\t\t\/\/sets this guys value to the correct part of the array\r\n\t\t\t_root['value'+row].thisValue = values[i];\r\n\t\t\t\/\/move onto the next row\r\n\t\t\trow ++;\r\n\t\t}\r\n\t}\r\n\t\/\/then we reset the rows so we can use the variable again\r\n\trow=0;\r\n}\r\n\/\/we have to remove movieclips from the stage if we're going to paginate\r\nfunction removeValues():Void{\r\n\t\/\/its a simple function, hopefully you can understand it\r\n\tfor(i=0;i<limit;i++){\r\n\t\t_root['value'+i].removeMovieClip();\r\n\t}\r\n}\r\n\/\/adding functions to the buttons when they're clicked\r\nmcPrev.onRelease = function(){\r\n\t\/\/if the page isn't too low\r\n\tif(page > 1){\r\n\t\t\/\/then go to the previous page\r\n\t\tremoveValues();\r\n\t\tpage --;\r\n\t\tcreateValues();\r\n\t\t\/\/updating the text\r\n\t\tpageTxt = 'Page '+page+' out of '+totalPages;\r\n\t}\r\n}\r\n\r\nmcNext.onRelease= function(){\r\n\t\/\/if the page isn't too high\r\n\tif(page < totalPages){\r\n\t\t\/\/then go to the next page\r\n\t\tremoveValues();\r\n\t\tpage ++;\r\n\t\tcreateValues();\r\n\t\t\/\/updating the text\r\n\t\tpageTxt = 'Page '+page+' out of '+totalPages;\r\n\t}\r\n}\r\n\r\n\/\/we're going to have to show the values first thing\r\ncreateValues();\r\n<\/pre>\n<p>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.<\/p>\n<h3>The Final Product<\/h3>\n<p><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"550\" height=\"400\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/paginate-as2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/paginate-as2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/paginate-as2\/source.fla\">Source .fla file<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,7,8,65,4,68,42],"tags":[25,7,19,137,22],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/545"}],"collection":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/comments?post=545"}],"version-history":[{"count":7,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/545\/revisions"}],"predecessor-version":[{"id":579,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/545\/revisions\/579"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}