国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android基本游戲循環實例分析

2020-04-11 11:24:03
字體:
來源:轉載
供稿:網友

本文實例講述了Android基本游戲循環。分享給大家供大家參考。具體如下:

// desired fpsprivate final static int  MAX_FPS = 50;// maximum number of frames to be skippedprivate final static int  MAX_FRAME_SKIPS = 5;// the frame periodprivate final static int  FRAME_PERIOD = 1000 / MAX_FPS; @Overridepublic void run() {  Canvas canvas;  Log.d(TAG, "Starting game loop");  long beginTime;   // the time when the cycle begun  long timeDiff;   // the time it took for the cycle to execute  int sleepTime;   // ms to sleep (<0 if we're behind)  int framesSkipped; // number of frames being skipped   sleepTime = 0;  while (running) {    canvas = null;    // try locking the canvas for exclusive pixel editing    // in the surface    try {      canvas = this.surfaceHolder.lockCanvas();      synchronized (surfaceHolder) {        beginTime = System.currentTimeMillis();        framesSkipped = 0; // resetting the frames skipped        // update game state        this.gamePanel.update();        // render state to the screen        // draws the canvas on the panel        this.gamePanel.render(canvas);        // calculate how long did the cycle take        timeDiff = System.currentTimeMillis() - beginTime;        // calculate sleep time        sleepTime = (int)(FRAME_PERIOD - timeDiff);        if (sleepTime > 0) {          // if sleepTime > 0 we're OK          try {            // send the thread to sleep for a short period            // very useful for battery saving            Thread.sleep(sleepTime);          } catch (InterruptedException e) {}        }        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {          // we need to catch up          // update without rendering          this.gamePanel.update();          // add frame period to check if in next frame          sleepTime += FRAME_PERIOD;          framesSkipped++;        }      }    } finally {      // in case of an exception the surface is not left in      // an inconsistent state      if (canvas != null) {        surfaceHolder.unlockCanvasAndPost(canvas);      }    }  // end finally  }}

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河池市| 许昌市| 昌宁县| 米脂县| 综艺| 靖远县| 寻乌县| 安图县| 静乐县| 米林县| 利辛县| 衡南县| 永登县| 定日县| 马公市| 图木舒克市| 淮滨县| 绵竹市| 平潭县| 大同县| 南宁市| 利辛县| 邵阳县| 舟曲县| 綦江县| 普宁市| 吴江市| 合川市| 兰考县| 靖州| 白城市| 鄂伦春自治旗| 图木舒克市| 萝北县| 枝江市| 新民市| 全椒县| 彩票| 陵川县| 新巴尔虎右旗| 娱乐|