天天看点

开发游戏里做的一个背包数据分页,有不足处请指出

package share

{

 import flash.events.Event;

 public class Pages

 {

  [Bindable]

  public var pageRecordes:uint = 0 ;

  [Bindable]

  public var totalRows:uint = 0 ;       //总共多少行数据

  [Bindable]

  public var totalPages:uint = 0 ;      //总共多少页

  [Bindable]

  public var currentPage:uint = 1 ;     //当前页数

  [Bindable]

  private var pageStartRow:uint = 1 ;   //开始数据行

  [Bindable]

  private var pageEndRow:uint = 0 ;     //结束数据行

  [Bindable]

  public var source:Array ;     //传入的数组

  [Bindable]

  public var data:Array ;      //传给数据绑定源的数组

  public function Pages( pagerecord:uint,arr:Array )

  {

   this.pageRecordes = pagerecord ;

   this.source = arr ;

  }

  public function initApp( event:Event ):void

  {

   currentPage = 1 ;

   totalRows = source.length ;

   if( totalRows >= pageRecordes )

   {

    data = source.slice( 0,pageRecordes ) ;

   }else{

    data = source.slice( 0,totalRows ) ;

   }

   //根据收到的数据长度判断分为几页

   if( totalRows % pageRecordes == 0 )

   {

    totalPages = Math.floor( totalRows / pageRecordes ) ;

   }else{

    totalPages = Math.floor( totalRows / pageRecordes + 1 ) ;

   }

  }

  //上一页

   public function showPreviousPage():void

   {

    if( currentPage - 1 <= 0 )

    {

     return;

    }else{

     currentPage = currentPage - 1 ;

       setCurrentData();

    }

    }

     //下一页

   public function showNextPage():void

   {

       if( currentPage + 1 > totalPages )

       {

        return;

       }else{

        currentPage = currentPage + 1 ;

        setCurrentData();

       }

  }

  //读取每页数据

  private function setCurrentData():void

  {

   if ( currentPage == totalPages ) {

        pageStartRow = ( currentPage - 1 ) * pageRecordes + 1 ;

        pageEndRow = totalRows;

      }else{

        pageStartRow = ( currentPage - 1 ) * pageRecordes + 1 ;

        pageEndRow = currentPage * pageRecordes ;

      }

           data = source.slice( pageStartRow - 1,pageEndRow ) ;

  }

  //第一页

  public function firstPage():void

  {

   currentPage = 1 ;

   if( totalRows < pageRecordes )

   {

    data = source.slice( 0,totalRows ) ;

   }else{

    data = source.slice( 0,pageRecordes ) ;

   }

  }

  //最后页

  public function lastPage():void

  {

   currentPage = totalPages;

   if( totalRows <= pageRecordes )

   {

    data = source.slice( 0,totalRows ) ;

   }else{

    data = source.slice( ( totalPages - 1 ) * pageRecordes,totalRows ) ;

   }

  }

  //创建分页

  public static function createPage( pageRocord:uint,source:Array ):Pages

  {

   var p:Pages = new Pages( pageRocord,source ); //初始化分页

   return p ;

  }

 }

}

上一篇: FPS counter

继续阅读