Android基本游戏循环实例分析

作者:红薯 时间:2021-12-26 12:06:22 

本文实例讲述了Android基本游戏循环。分享给大家供大家参考。具体如下:


// desired fps
private final static int  MAX_FPS = 50;
// maximum number of frames to be skipped
private final static int  MAX_FRAME_SKIPS = 5;
// the frame period
private final static int  FRAME_PERIOD = 1000 / MAX_FPS;
@Override
public 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程序设计有所帮助。

标签:Android,游戏,循环
0
投稿

猜你喜欢

  • Android 8.0实现蓝牙遥控器自动配对

    2021-08-05 08:24:16
  • Java SpringBoot实现AOP

    2023-05-31 05:49:30
  • C# wpf Grid中实现控件拖动调整大小的示例代码

    2023-05-15 17:12:03
  • Java常问面试内容--数组、声明、初始化、冒泡、多维数组、稀疏数组

    2021-08-20 02:05:52
  • 通过Session案例分析一次性验证码登录

    2023-04-27 06:25:51
  • Java matches类,Pattern类及matcher类用法示例

    2022-02-21 22:39:52
  • Java4Android开发教程(一)JDK安装与配置

    2022-02-04 22:33:12
  • C# BinaryReader实现读取二进制文件

    2021-05-26 21:07:20
  • C#泛型详解及关键字作用

    2023-04-07 20:23:12
  • Springboot创建项目的图文教程(idea版本)

    2022-09-30 13:30:17
  • SpringBoot超详细讲解@Value注解

    2022-03-06 12:05:36
  • JDK 7U15在 Windows x86平台下的安装方法

    2023-04-09 07:31:08
  • Mybatis Plus中的流式查询案例

    2023-08-18 16:35:13
  • 详解java 中Spring jsonp 跨域请求的实例

    2023-11-19 02:48:18
  • C#(WinForm) ComboBox和ListBox添加项及设置默认选择项

    2022-05-13 10:09:49
  • RecylerView实现流布局StaggeredGridLayoutManager使用详解

    2023-04-30 20:51:38
  • 算法证明每一位都相同十进制数不是完全平方数

    2022-06-21 23:00:46
  • C#中数组初始化与数组元素复制的方法

    2023-05-14 15:45:06
  • Android自定义控件实现带数值和动画的圆形进度条

    2021-09-09 22:02:46
  • Android Zipalign工具优化Android APK应用

    2021-09-15 13:16:27
  • asp之家 软件编程 m.aspxhome.com