why game is running slow in libgdx?

别等时光非礼了梦想. 提交于 2019-11-27 02:01:07

问题


I am making racing game in Libgdx.My game apk size is 9.92 mb and I am using four texture packer of total size is 9.92 Mb. My game is running on desktop but its run on android device very slow. What is reason behind it?


回答1:


There are few loopholes which we neglect while programming. Desktop processors are way more powerful so the game may run smoothly on Desktop but may slow on mobile Device.

Here are some key notes which you should follow for optimum game flow:

  1. No I/O operations in render method.
  2. Avoid creating Objects in Render Method.
  3. Objects must be reused (for instance if your game have 1000 platforms but on current screen you can display only 3, than instead of making 1000 objects make 5 or 6 and reuse them). You can use Pool class provided by LibGdx for object pooling.
  4. Try to load only those assets which are necessary to show on current screen.

Try to check your logcat if the Garbage collector is called. If so than try to use finalize method of object class to find which class object are collected as garbage and try to improve on it.

Good luck.




回答2:


I've got some additional tips for improving performance:

  1. Try to minimize texture bindings (or generally bindings when you're making a 3D game for example) in you render loop. Use texture atlases and try to use one texture after binding as often as possible, before binding another texture unit.
  2. Don't display things that are not in the frustum/viewport. Calculate first if the drawn object can even be seen by the active camera or not. If it's not seen, just don't load it onto your GPU when rendering!
  3. Don't use spritebatch.begin() or spritebatch.end() too often in the render loop, because every time you begin/end it, it's flushed and loaded onto the GPU for rendering its stuff.
  4. Do NOT load assets while rendering, except you're doing it once in another thread.
  5. The latest versions of libgdx also provide a GLProfiler where you can measure how many draw calls, texture bindings, vertices, etc. you have per frame. I'd strongly recommend this since there always can be situations where you would not expect an overhead of memory/computational usage.
    1. Use libgdx Poolable (interface) objects and Pool for pooling objects and minimizing the time for object creation, since the creation of objects might cause tiny but noticable stutterings in your game-render loop

By the way, without any additional information, no one's going to give you a good or precise answer. If you think it's not worth it to write enough text or information for your question, why should it be worth it to answer it?




回答3:


To really understand why your game is running slow you need to profile your application.

There are free tools avaiable for this.

On Desktop you can use VisualVM. On Android you can use Android Monitor.

With profiling you will find excatly which methods are taking up the most time.

A likely cause of slowdowns is texture binding. Do you switch between different pages of packed textures often? Try to draw everything from one page before switching to another page.




回答4:


The answer is likely a little more that just "Computer fast; phone slow". Rather, it's important to note that your computer Java VM is likely Oracles very nicely optimized JVM while your phone's Java VM is likely Dalvik, which, to say nothing else of its performance, does not have the same optimizations for object creation and management.

As others have said, libGDX provides a Pool class for just this reason. Take a look here: https://github.com/libgdx/libgdx/wiki/Memory-management




回答5:


One very important thing in LibGDX is that you should make sure that sometimes loading assets from the memory cannot go in the render() method. Make sure that you are loading the assets in the right times and they are not coming in the render method.

Another very important thing is that try to calculate your math and make it independent of the render in the sense that your next frame should not wait for calculations to happen...!

These are the major 2 things i encountered when I was making the Snake game Tutorial.
Thanks,
Abhijeet.




回答6:


One thing I have found, is that drawing is laggy. This means that if you are drawing offscreen items, then it uses a lot of useless resources. If you just check if they are onscreen before drawing, then your performance improves by a lot surprisingly.




回答7:


Points to ponder (From personal experience)

  1. DO NOT keep calling a function,in render method, that updates something like time,score on HUD (Make these updates only when required eg when score increases ONLY then update score etc)

  2. Make calls IF specific (Make updations on certain condition, not all the time) eg. Calling/updating in render method at 60FPS - means you update time 60 times a sec when it just needs to be updated once per sec )

These points will effect hugely on performance (thumbs up)




回答8:


You need to check the your Image size of the game.If your image size are more than decrease the size of images by using the following link "http://tinypng.org/". It will be help you.



来源:https://stackoverflow.com/questions/17347883/why-game-is-running-slow-in-libgdx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!