如何在Java中加载Spritesheet时防止FPS下降? [英] How do prevent my FPS dropping when loading spritesheet in java?

查看:91
本文介绍了如何在Java中加载Spritesheet时防止FPS下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我已经有一段时间进行Java游戏编程了,最近我遇到了一个问题.当我在Java中加载相对较小的Sprite工作表时,我的FPS从500上升到250,这使我的游戏变得很落后.如果有人知道减少Java游戏延迟并获得更好的FPS的方法,那就太好了!

Hey guys i have been java game programming fr a while and i hava had a problem lately. When i load a relatively small sprite sheet in java my FPS goes from 500 to 250 and makes my game pretty laggy. If anyone knew a way to make a java game less laggy and get better FPS, that would be great!

    BufferedImage spriteSheet = ImageIO.read(new File(spriteSheetLocation));
    sprites = new BufferedImage[rows * columns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width,
                    j * height, width, height);
        }
    }

推荐答案

通常,在开发游戏时,您会cache所有sprites(或几乎是),因此一开始只加载一次.

Normally when developing games, you cache all your sprites (or almost) so you only load them once at the beginning.

在我进行游戏开发的那段时间里,我使用缓存编辑器(很容易创建自己的缓存编辑器,但是如果您想节省时间,那么网络上可能有很多),然后将每个精灵存储在一个文件,以字节为单位(序列化的对象).然后,该文件由缓存编辑器压缩,每个玩家将下载一次并将其存储在计算机上的某个位置(他们不知道它是完成的,而是由游戏来处理的.)

Back in the days when I was doing game development, I was using cache editor (it is easy to create your own, but there is probably plenty on web if you want to save time) then you store each of your sprites in a file as bytes (serialized objects). Then this file is compressed by the cache editor and each players would download it once and store somewhere on the computer (they have no idea it is done, it is handled by the game).

然后,当玩家不得不加载新的精灵时,它已经缓存在计算机上,并且速度更快.

Then, when players had to load a new sprites it was already cached on the computer and was very faster.

这篇关于如何在Java中加载Spritesheet时防止FPS下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆