Android基本游戏循环实例分析
作者:红薯 发布时间:2021-12-26 12:06:22
标签:Android,游戏,循环
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 直接上代码:public class WeiXinFilter implements Filter{private static Logge
- 此方法是通过view的方式获取当前activity的屏幕截图,并不是framebuffer的方式,所以有一定的局限性。但是这种方法相对简单,
- properties和yml的区别这几天刚好看到Spring Boot当中有两种配置文件的方式,但是这两种配置方式有什么区别呢?proper
- NTP是Android原生通过网络获取时间的机制,其中关键代码逻辑都在NetworkTimeUpdateService,它是Android系
- 继承的概念及定义概念:继承机制是面向对象程序设计为了提高代码复用率的一种手段,它可以保持原类特性的基础上进行拓展,简单来说继承是类层次的复用
- 对于javascript的冒泡,我一直误解它了,冒泡,即是从底层往外blow blow blow ...惭愧的是,我一直以为阻止冒泡是阻止父
- 本文实例为大家分享了Android使用GridView实现横向滚动效果的具体代码,供大家参考,具体内容如下第一次做横向滑动,看了一些列子,基
- 这里直接给出C#类成员一般初始化顺序:子类静态字段子类静态构造子类实例字段父类静态字段父类静态构造父类实例字段父类实例构造子类实例构造为什么
- 本篇博客要分享的一个UI效果——实现底部切换标签,想必大家在一些应用上面遇到过这种效果了,最典型的就是微信了,可以左右滑动切换页面,也可以点
- JRebel 介绍IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊。目
- 本文实例讲述了C#实现动态加载dll的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Co
- 当我们要创建一个Tcp/Ip Server connection ,我们需要一个范围在1000到65535之间的端口 。但是本机一个端口只能
- 在高并发的系统中,往往需要在系统中做限流,一方面是为了防止大量的请求使服务器过载,导致服务不可用,另一方面是为了防止网络攻击。常见的限流方式
- 用户User的注册类型有Super和Common两种public eumn RegistrationType{ &nb
- 废话不多说,咱们第一篇文章就是模仿“知乎”的回答详情页的动画效果,先上个原版的效果图,咱们就是要做出这个效果 &nbs
- 1、找准入口,使用ClassPathXmlApplicationContext的构造方法加载配置文件,用于加载classPath下的配置文件
- 一. 先来一段代码我们先上一段代码:String str1 = new StringBuilder("你好").appe
- 说明:在填写表数据时当输入完一个文本框后,输入下一个文本框时需要用Tab键切换,但是有的人喜欢用Enter键切换下一个,此方法是Enter取
- 目录概要独立文件专属文件internal storageexternal storage概要当我们查看手机的文件管理器的时候,会发现里面的文
- 废话不多说了,给大家贴关键代码了,具体代码如下所示:import java.io.File;import java.io.FileOutpu