1

So I am having problem with game lag at the beginning of my project. So as soon as you tap to start the game it starts running on about 42-48 FPS, as you go on the game, it starts settling and ends up on about 58-59 FPS which is good. So I have been trying to fix it, I already tried by pre loading my texture atlases but it is still not working. So I ran the time profiler and it appears this is my problem:

It seems to be its my Update method. What I believe the problem is, it has to be with my Plist file. I am loading all my objects from one really long file. I tried running the project on a much shorter file and it runs perfectly at 60 FPS. Is there any way in which I can solve this?

2
  • Maybe you could split the plist up into sections and then only parse the first section when the game starts. The other sections could then possibly be parsed async in the background.
    – donnywals
    May 6, 2015 at 8:16
  • thanks man do you have any idea on how can I do it? Or some resources where I can read about it and do something like that May 9, 2015 at 1:07

3 Answers 3

2

There are a couple of things which cause an initial drop in FPS. Before game play starts:

  1. Preload textures and texture atlases.
  2. Preload any sounds you are using.
  3. Preload any additional resources (plist, building maps, etc...)

In general you should always preload all assets before allowing game play to start.

2
0

You should probably use different sections for each pattern in the game.

In Mega Jump, different patterns are randomly loaded dynamically. This means, you can have different plists for different patterns, and as you go up, you can dealloc offscreen content and alloc new parts of the game from a random plist.

Always load things dynamically (possibly async) so that you never waste memory loading offscreen content that won't be used until later on.

0

So I am using the time profiler to see what is is that is making my FPS to drop down at the beginning and I believe according to it is this piece of code in my Update method:

Always be careful with the update method. Looping through and processing many nodes every frame can create overhead (and in the code you posted you are enumerating twice!).

Try adding a counter and run the code you posted less often and see how that impacts your FPS. For example, you could choose to run the code every 0.1 seconds instead of every 1/60 seconds.

In general you should try to avoid running code at the frame rate if you don't need to. For example, your game could run at 60 frames a second but you could have some game logic that runs every 0.1 as long as the gameplay is not affected. These are important optimizations to look for when designing a game.


Reading more into your description it looks like your problem might also be related to not preloading your assets correctly. Make sure you keep a single reference to your SKTextures, Fonts, Sounds, Actions and load them into memory before starting the game.

3
  • Hey @EpicByte and how may I add a counter? To run the code as you are saying. And as for the SKTextures I am already preloading them before starting the game but still that didn't work! May 9, 2015 at 4:17
  • @Thesaurus03 You could make an integer and increment it each time update is called. Then once the counter is greater then a certain value run the code and reset the counter. You could also calculate the delta time of each frame and use that instead. As for preloading, I have a feeling you are preloading but not keeping single references to your textures. Meaning if you are initializing SKTextures like SKTexture(...) anywhere in your game that would be bad. Initialize all textures in the beginning and have all your code reuse the variables.
    – Epic Byte
    May 9, 2015 at 13:56
  • @Thesaurus03 furthermore, if you have a lot of textures you could store them all in a dictionary or array. And any textures you use for animation should be in its own separate array.
    – Epic Byte
    May 9, 2015 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.