天天看點

開發遊戲裡做的一個背包資料分頁,有不足處請指出

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

繼續閱讀