ITeye博客

关于“数据分页”

wgcode · 2010-12-25 14:00 · Flash/AS3 · 编程语言 · Markdown

分页是游戏和应用程序开发中经常需要用到的一个程序,
在游戏开发中我用得最多的算是数组(ArrayPages)分页和哈希表(HashMapPages)分页。
比如《抢车位》游戏中的“场景店”模块、《QQ农场》中的好友列表、常见新闻列表分页等等。

下面我提供之前写的ArrayPages.as ,哈希表的分页就类似。

AS3代码

  1. package com.lbynet.util
  2. {
  3. /**
  4. * ...
  5. * @author lbynet (Tools -> 数组数据分页...)
  6. * @version 0.2
  7. * @copy shch8.com All Rights Reserved At 2008
  8. */
  9. public class ArrayPages
  10. {
  11. private var data_array:Array; // 存储所有数据
  12. private var pageRecord:int;// 每页的数据个数
  13. private var arrayLength:int;// 总个数
  14. public var totalPages:int; // 总页数
  15. public function ArrayPages(_data:Array, _pagerecord:int = 4)
  16. {
  17. if (_data != null)
  18. {
  19. this.data_array = _data;
  20. this.pageRecord = _pagerecord;
  21. this.arrayLength = this.data_array.length;
  22. this.totalPages = this.arrayLength / this.pageRecord;
  23. if (this.arrayLength % this.pageRecord > 0)
  24. {
  25. this.totalPages += 1;
  26. }
  27. }
  28. //trace(this.data_array);
  29. //trace(this.pageRecord);
  30. //trace(this.arrayLength);
  31. //trace(this.totalPages);
  32. }
  33. /* 取出某一页的数据 (1-totalPages) */
  34. public function getPageData(curpage:uint):Array
  35. {
  36. if (curpage <= 0||curpage > this.totalPages)
  37. {
  38. throw new Error('页数必需在 1 - ' + this.totalPages + ' 页 以内');
  39. }
  40. var _startIndex:int;// 某页的第一条数据
  41. if (curpage == 1)
  42. {
  43. _startIndex = 0;
  44. }
  45. else
  46. {
  47. _startIndex = (curpage - 1) * pageRecord;
  48. }
  49. var _endIndex:int = _startIndex + pageRecord; // 某页的最后一条数据
  50. if (curpage >= this.totalPages && _endIndex != this.arrayLength)// (_endIndex != this.arrayLength)  === (this.arrayLength % this.pageRecord == 0)
  51. {
  52. _endIndex = _startIndex + this.arrayLength % this.pageRecord;
  53. }
  54. //trace('                           ', _startIndex, _endIndex);
  55. return data_array.slice(_startIndex, _endIndex);
  56. }
  57. }
  58. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。