0
\$\begingroup\$

I'm working on a game with Objective-C and Cocos2d, the game has a huge number of images, and obviously as I add images on the spritesheet (well yes I'm using spritesheets) the loading time get longer, I've seen a huge increasing in loading time since the last update (I had to add a lot of stuff) and that's why I truly need to preload the entire spritesheet before the game start. So here is what I don't know exactly how to do, I'm using this method in the appDelegate in the applicationDidFinishLaunchingWithOptions method:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spriteSheetFile.plist"];

but seems to do nothing. There is surely something I'm missing...

I've also tried:

[[CCTextureCache sharedTextureCache] addImageAsync:image_png target:self selector:@selector(loadImage:)];

but to be honest I'm not sure on how to use this with a spritesheet...

so my question is: what here is wrong and how can I preload a spritesheet full of sprites before the game start? and also what I'm not totally sure about is, if I preload a spritesheet, this means that when the game start the sprite needs to be draw the first time, there is where the loading comes from? how can I make this scene loading as fastest as It can?

any help would be great, thanks

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This question is several weeks old, but I thought I would answer in the event you were still looking for assistance, or too perhaps help someone else down the line.

Please note that the way you are loading the spritesheet using CCSpriteFrameCache is correct, but it does nothing more than add the contents of the spritesheet to memory. You then need to create sprites from that loaded content using CCSprite * mySprite = [CCSprite spriteWithSpriteFrameName:@"spritename.png"], for example. I assume you know how to add sprites as children of a layer or how to use CCSpriteBatchNode. The latter is where you get the performance boost.

Once a spritesheet has been loaded into memory you can access its contents from virtually anywhere within your application. As such you could, if you wanted to, load all spritesheets at the time your application launches or at any point a pregame launch screen is displayed; thus this resource load would occur prior to the actual game start. Then you would only need to create your needed sprites for actual game play.

A potential problem with this approach, however, would be if you had many spritesheets and not all of them would be needed for each game "level" for lack of a better term. The more spritesheets you load, the more memory they take. So you have to balance this preload approach with any performance issues that may arise.

Let me know if this helps.

\$\endgroup\$
1
  • \$\begingroup\$ Pluvius' answer is correct, and for more details on the different approaches that you can use to pre-load your textures, you can refer to this answer I gave to a related question: gamedev.stackexchange.com/questions/47247/… \$\endgroup\$
    – Jamornh
    Sep 10, 2013 at 5:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .