Saturday, September 5, 2015

Cocos2d patterns for simple texture management


I'd like to simplify the way I preload image resources in my Cocos2d based iOS apps. I've created a small object the just wraps a few of CCTextureCache's methods. It looks like this:


@interface TextureHelper : NSObject
+ (void) preloadTextures;
+ (CCTexture2D *) textureForImageNamed:(NSString *) imageName;
@end

@implementation TextureHelper
+ (CCTexture2D *) textureForImageNamed:(NSString *) imageName {

return [[CCTextureCache sharedTextureCache] textureForKey:imageName];
}

+ (void) preloadTextures {
// or could read from a directory...
NSArray *imageNames = @[
@"myImage1.png",
@"myImage2.png"
];


for (NSString *imageName in imageNames)
[[CCTextureCache sharedTextureCache] addImage:imageName];
}
@end

The goal is to preload all images at start-up. Then during game play any sprites created on the fly won't cause images to be read from disk. What are some other more common and/or more effective patterns for achieving this?




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...