简体   繁体   English

游戏编程性能

[英]Game Programming Performance

I have narrowed down my slowdowns to this method 我已经缩减了这种方法的速度

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);
    }
}

This is taking about 3 seconds to run! 运行大约需要3秒钟! any help would be appreciated the bitmap is about twice the size of the screen but this is key to my game so I need to keep this :) any help on optimising? 任何帮助将不胜感激位图大约是屏幕大小的两倍,但这是我的游戏的关键,所以我需要保持这个:)任何优化的帮助? thanks 谢谢

you always wait a fixed time here, independent of how long the whole calculations and ui updates take 你总是在这里等待一段固定的时间,而不管整个计算和ui更新需要多长时间

 frame.postDelayed(frameUpdate, FRAME_RATE);

what you want to do is: before the new "game tick" you create a timestamp variable, then you do the calculations, and then you check how much time is left until the next tick should be. 你想要做的是:在新的“游戏勾号”之前你创建一个时间戳变量,然后你进行计算,然后你检查剩下多少时间,直到下一个滴答应该是。

something like this: 这样的事情:

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);

}

this way, the update ticks should always be equally timed (assumed that the device is capable of running the code within the framerate value) 这样,更新标记应始终保持相同的时间(假设设备能够在帧速率值内运行代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM