游戏编程性能



我已经将我的减速范围缩小到这种方法

public void drawMap(Canvas canvas) {
    if (car.x > 0 && map != null) {
        clipArea = Bitmap.createBitmap(map, mapx, mapy, getWidth(),
                getHeight());
        canvas.drawBitmap(clipArea, 0, 0, null);
    }
}

这大约需要 3 秒才能运行! 任何帮助将不胜感激 位图大约是屏幕大小的两倍,但这是我的游戏的关键,所以我需要保持这个:)对优化有什么帮助吗?谢谢

您总是在这里等待固定的时间,与整个计算和 UI 更新所需的时间无关

 frame.postDelayed(frameUpdate, FRAME_RATE);

你要做的是:在新的"游戏滴答声"之前,你创建一个时间戳变量,然后你进行计算,然后你检查距离下一个即时报价还剩多少时间。

像这样:

synchronized public void run() {

    Date timestamp = new Date();
    // do your calculations
    ...
    // calculate how much time it took and when the next update should be:
    int timeToNextTick = FRAME_RATE - (new Date().getTime() - timestamp.getTime());
    frame.postDelayed(frameUpdate, timeToNextTick);
}

这样,更新时钟周期应始终等时(假设设备能够在帧速率值内运行代码)

最新更新