<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      【CSON原創(chuàng)】HTML5游戲框架cnGameJS開發(fā)實錄(游戲循環(huán)篇)

       返回目錄

        由于整個游戲都在一個游戲循環(huán)中進(jìn)行,所以游戲循環(huán)可以說是游戲的核心部分。每次循環(huán)時,更新游戲?qū)ο蟮膶傩裕约袄L制游戲元素。

        在之前的資源加載篇已經(jīng)提到過,在資源加載完成后,啟動游戲的同時會啟動一個游戲循環(huán),現(xiàn)在再來回顧這部分代碼:

       

          /**
      *圖像加載完畢的處理程序
      *
      */
      var imgLoad=function(self){
      return function(){
      self.loadedCount+=1;
      self.loadedImgs[this.srcPath]=this;
      this.onLoad=null; //保證圖片的onLoad執(zhí)行一次后銷毀
      self.loadedPercent=Math.floor(self.loadedCount/self.sum*100);
      self.onLoad&&self.onLoad(self.loadedPercent);
      if(self.loadedPercent===100){
      self.loadedCount=0;
      self.loadedPercent=0;
      loadingImgs={};
      if(self.gameObj&&self.gameObj.initialize){
      self.gameObj.initialize();
      if(cg.loop&&!cg.loop.stop){//結(jié)束上一個循環(huán)
      cg.loop.end();
      }
      cg.loop=new cg.GameLoop(self.gameObj);//開始新游戲循環(huán)
      cg.loop.start();
      }

      }


      }
      }

        圖像資源加載完畢后,調(diào)用游戲?qū)ο蟮膇nitialize方法,并且判斷游戲循環(huán)對象是否存在,如果存在,則結(jié)束上一個循環(huán)(這種情況一般在切換關(guān)卡,傳入新的游戲?qū)ο髸r出現(xiàn)),否則建立并開始游戲循環(huán)。

        好了,現(xiàn)在正式來看看游戲循環(huán)的實現(xiàn)代碼:

          var gameLoop=function(gameObj,options){

      if(!(this instanceof arguments.callee)){
      return new arguments.callee(gameObj,options);
      }
      this.init(gameObj,options);
      }

        首先游戲循環(huán)函數(shù)也必須保證是以構(gòu)造函數(shù)的形式調(diào)用,在調(diào)用之后,為對象初始化:

              /**
      *初始化
      *
      */
      init:function(gameObj,options){
      /**
      *默認(rèn)對象
      *
      */
      var defaultObj={
      fps:30
      };
      options=options||{};

      options=cg.core.extend(defaultObj,options);
      this.gameObj=gameObj;
      this.fps=options.fps;
      interval=1000/this.fps;

      this.pause=false;
      this.stop=true;
      },

        用戶需要設(shè)置的參數(shù)只有一個,就是fps(frame per second),該參數(shù)是每秒鐘執(zhí)行的幀的次數(shù),根據(jù)該參數(shù),我們可以計算出多少毫秒執(zhí)行一次游戲循環(huán)(interval參數(shù))。另外循環(huán)支持暫停和停止兩種模式。

        

              /**
      *開始循環(huán)
      *
      */
      start:function(){
      if(this.stop){ //如果是結(jié)束狀態(tài)則可以開始
      this.stop=false;

      this.now=new Date().getTime();
      this.startTime=new Date().getTime();
      this.duration=0;
      loop.call(this)();
      }
      },

        當(dāng)循環(huán)開始,我們可以保存開始的時間,這樣就可以不斷更新循環(huán)所經(jīng)歷的時間(duration)。之后調(diào)用loop這個似有函數(shù),實現(xiàn)循環(huán)。

          var timeId;
      var interval;
      /**
      *循環(huán)方法
      *
      */
      var loop=function(){
      var self=this;
      return function(){
      if(!self.pause&&!self.stop){

      self.now=new Date().getTime();
      self.duration=self.startTime-self.now;

      if(self.gameObj.update){
      self.gameObj.update();
      }
      if(self.gameObj.draw){
      cg.context.clearRect(0,0,cg.width,cg.height);
      self.gameObj.draw();
      }
      }
      timeId=window.setTimeout(arguments.callee,interval);
      }
      }

        如果不是暫停或停止,則調(diào)用游戲?qū)ο蟮膗pdate和draw(注意游戲?qū)ο蟮膗pdate負(fù)責(zé)調(diào)用該關(guān)卡所有元素的update,draw也一樣)。之后調(diào)用setTimeout遞歸調(diào)用自己,實現(xiàn)循環(huán)。

       

      游戲循環(huán)所有源碼:

      /**
      *
      *游戲循環(huán)
      *
      *
      */
      cnGame.register("cnGame",function(cg){

      var timeId;
      var interval;
      /**
      *循環(huán)方法
      *
      */
      var loop=function(){
      var self=this;
      return function(){
      if(!self.pause&&!self.stop){

      self.now=new Date().getTime();
      self.duration=self.startTime-self.now;

      if(self.gameObj.update){
      self.gameObj.update();
      }
      if(self.gameObj.draw){
      cg.context.clearRect(0,0,cg.width,cg.height);
      self.gameObj.draw();
      }
      }
      timeId=window.setTimeout(arguments.callee,interval);
      }
      }

      var gameLoop=function(gameObj,options){

      if(!(this instanceof arguments.callee)){
      return new arguments.callee(gameObj,options);
      }
      this.init(gameObj,options);
      }
      gameLoop.prototype={
      /**
      *初始化
      *
      */
      init:function(gameObj,options){
      /**
      *默認(rèn)對象
      *
      */
      var defaultObj={
      fps:30
      };
      options=options||{};

      options=cg.core.extend(defaultObj,options);
      this.gameObj=gameObj;
      this.fps=options.fps;
      interval=1000/this.fps;

      this.pause=false;
      this.stop=true;
      },

      /**
      *開始循環(huán)
      *
      */
      start:function(){
      if(this.stop){ //如果是結(jié)束狀態(tài)則可以開始
      this.stop=false;

      this.now=new Date().getTime();
      this.startTime=new Date().getTime();
      this.duration=0;
      loop.call(this)();
      }
      },
      /**
      *繼續(xù)循環(huán)
      *
      */
      run:function(){
      this.pause=false;
      },
      /**
      *暫停循環(huán)
      *
      */
      pause:function(){
      this.pause=true;
      },
      /**
      *停止循環(huán)
      *
      */
      end:function(){
      this.stop=true;
      window.clearTimeout(timeId);
      }


      }
      this.GameLoop=gameLoop;
      });







      posted @ 2012-02-14 12:43  Cson  閱讀(2236)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 欧美颜射内射中出口爆在线| 中文字幕在线无码一区二区三区| 幻女free性俄罗斯毛片| 日韩丝袜亚洲国产欧美一区| 亚洲人妻系列中文字幕| 一区二区三区精品偷拍| 风韵丰满熟妇啪啪区老熟熟女 | 樱花草在线社区www| 久久亚洲精品天天综合网| 成人欧美一区二区三区在线观看| 人妻熟妇乱又伦精品无码专区| 潘金莲高清dvd碟片| 国产精品三级黄色小视频| 国产粉嫩学生高清专区麻豆| 91福利一区福利二区| 日夜啪啪一区二区三区| 中文字幕乱码视频32| 久久亚洲中文无码咪咪爱| 老子午夜精品无码| 国产精品午夜福利精品| 少妇激情a∨一区二区三区| 亚洲欧美成人a∨观看| 久久99精品久久久大学生| 国产伦码精品一区二区| 91色老久久精品偷偷性色| 日本一区二区三区免费播放视频站| 动漫av网站免费观看| 强奷漂亮人妻系列老师| 又黄又刺激又黄又舒服| 亚洲欧美在线观看一区二区| 一区二区三区四区黄色网| 性色欲情网站| 国产精品一区二区三区四区| 亚洲AVAV天堂AV在线网阿V| 2019国产精品青青草原| 亚洲无人区码一二三四区| 华人在线亚洲欧美精品| 日夜啪啪一区二区三区| 国产呦交精品免费视频| 毛片av中文字幕一区二区| 亚洲精品乱码免费精品乱|