2
votes

I'm writing a game with cocos2d and I have noticed that the frame rate drops as the game progresses. I've checked for leaks but everything looks fine so I'm at a loss as to what to do next. Sorry if this is a really basic question but what sort of factors cause frame rate loss?

The problem seems to get worse when I clear the sprites from a layer. Sometimes the framerate jumps back to 60 but occasionally it drops down to 30 or lower and never recovers. After a few minutes the animations are noticably slower and the game pretty much grinds to a halt. I'm not sure if this is specific to cocos2d or a common issue in game development, but it renders the game unplayable :(

4

4 Answers

3
votes

How are you testing for memory leaks? I don't think you should rule that out as the cause until you are absolutely certain because that sounds like classic "memory leak behaviour".

Also make sure you don't have something like a log or something in memory which is continually getting larger and larger every frame (I say so because that's happened to me in the past).

Also make sure you aren't continually loading new objects and/or sprites as your game progresses. Try and keep all your load calls in the initialization segment of your logic.

1
votes

Your framerate will drop if you add more sprites, obviously, because it has more to process.

Now, I do not know anything about cocos2d, but when you are "clearing the sprites from a layer", it sounds as though it simply hides them from view in addition to storing the fact that they are now hidden.

Maybe try and use fewer sprites?

1
votes

Are you reusing preloaded textures? If you aren't you should. Loading the same texture again and again is both slow, and is a leak.

Additionally, are you releasing textures you're done with? Even though the sprite is gone the texture is still there.

0
votes

I recently ran into a problem with Flash involving a progressive slowdown in graphics rasterization, so perhaps something similar could be happening here. A slowdown will generally occur when the system has to perform too much work. Memory leaks are obvious, because over-allocation and fragmentation of memory are never good, but consider this:

What I discovered was I had forgotton to call "clear" on the Graphics object, so every time I redrew the border of a sprite, it was drawing another rounded rectangle and the draw commands are cumulative. After a few dozen interactions, it was drawing this filled, rounded rectangle a hundred times over itself. The shaders, code, etc. were all taking up less than 1% of the rendering time, but that over-rasterization was killing my framerate.

So I would suggest checking for things like object creation, forgetting to clear things, drawing methods, etc. Make sure the system is not doing unnecessary work or accumulating unnecessary operations, not just memory objects.