0

n'tMy game scene is run from a level selection scene using replaceScene and and a level number is passed in. The game layer loads data from a plist and then creates objects in in the world space (CCSprite subclass with box2d bodies). The hero character remains in the center of the screen while the level is played - I adjust layer position to do this along with layer scale to zoom in and out based on the speed of movement.

For example:

CGPoint layerPosition = ccp(((screenSize.width / 2) - ourHero.position.x) * sceneScale, ((screenSize.height / 2) - ourHero.position.y) * sceneScale);
[self setPosition:layerPosition];
[self setScale:sceneScale];

The problem is, the first time the level runs movement can often be jerky when speed increases and the layer zooms out (not just for a few seconds at the start). However, if the user restarts the level (button executes replaceScene) the movement of the replacement level is perfectly smooth, even at speed.

I am using a batchnode and have the sprite images stored in a frame cache. I have tried setting up the frame cache and preloading sounds in the level selection scene, but that doesn't make any difference. I have event tried purging the cache between levels to see if I can make the restarted level exhibit the same behavior, but that didn't do anything.

Can anyone suggest what might cause this and how I might be able to eliminate it.

Thanks!

TinoK
  • 301
  • 1
  • 9
  • Of course the first time you load your game scene it will be slower, because all the objects HAVE to be created whereas the second time only SOME things need to be created. Anyway what I recommend is use Time Profiler and see what is causing the lag. From the sounds of it you are doing everything the right way so let Instruments do the talking. – SimplyKiwi Sep 23 '12 at 19:13
  • Thanks for the reply - Spending 58% on CCNode Vist (44% on CCSpriteBatchNode visit), 29% on EAGLView swapBuffers and around 1.9% on CCScheduler tick. – TinoK Sep 23 '12 at 19:40
  • What is odd though is that the jerky movement persists for the entire first play of the level - Surely all of the objects would have been created at some point and then the jerkiness would subside. Thanks again for any and all feedback!! – TinoK Sep 23 '12 at 19:42
  • Hmm thats odd. Your only using 1 sprite sheet right? Also check the update loop for anything that could be causing lagginess. – SimplyKiwi Sep 23 '12 at 20:18
  • Yep, 1 sprite sheet. When you say "update loop" what are you refering to? – TinoK Sep 23 '12 at 20:23
  • The game loop that gets called most likely at every 1/60th of a second. It is common among all games for iOS – SimplyKiwi Sep 23 '12 at 20:41

1 Answers1

0

Posible solution:

Load all texture and sprite sheet in init/onEnter. If not possible to load everything at once then use threaded loading. For audio/music, u can use preLoad calls in sound engine.

Here s my thread. Me also faced something same problem. Finally resolved by using separate thread for loading.

Community
  • 1
  • 1
Guru
  • 21,652
  • 10
  • 63
  • 102
  • 1
    Thanks for the suggested solution. Can I ask, did the behavior you observed continue for the entire run of the level (indefinitely) or did it stop after your asynchronous load completed? I ask because I am loading all textures and audio beforehand (level init) - It's still jerky the first time the layer scene runs. – TinoK Sep 23 '12 at 19:49
  • cocos2D animation with runAction ideally takes time in first time. Try to confirm that in ur case. try to initialize all cocos2D actions in init itself and play once(may be in outside the screen) with runAction call. In one of the game me also faced jerky in animation only in first time...then moved that animation initialization to init. Try to play once in init and then stop... – Guru Sep 24 '12 at 05:15