Search code examples
imagecocos2d-iphoneloadtextures

What happened to TextureMgr?


In cocos2d, there used to be a TextureMgr method (Async) that let you load images for later use. Now when I use TextureMgr, it says it's undeclared. Is it deprecated? I'm on .99.5. If it's no longer usable, what replaces it? Is there anything that can do the same as this line?

 [[TextureMgr sharedTextureMgr] addImageAsync:...NSString goes here... target:self selector:@selector(method:)];

Solution

  • take a look at CCTextureCache

    http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_texture_cache.html

    maybe it is you are looking for.

    This cache is used when your are creating any object weith a texture: sprite for example. And you can use it for precaching your images.

    EDIT: The CCTextureCache is used when your creating ang object with a texture, as i said - and so if the texture is already in cache the element creation is much more faster then if you would loading the texture first and then creating an object.

    For example if you are writing the code like this:

    id sprite = [CCSprite spriteWithFile: @"my-file.png"]

    and the @"my-file.png" texture is not in cache it will be loaded first and it will take some time (much more then just creating an object).

    If you are writing code like this:

    id sprite1 = [CCSprite spriteWithFile: @"my-file.png"];
    id sprite2 = [CCSprite spriteWithFile: @"my-file.png"];
    

    Then sprite1 will be created slow and sprite2 much more faster because the texture is already in cache.

    You can manuale add texture to cache

    [[CCTextureCache sharedTextureCache] addImage: @"my-file.png"];

    and the creating of all objects with this texture will be fast.

    The common place in code when you have to precache textures is game loading or level package loading or level loading.

    Also You can precache sounds if you need using them SimpleAudioEngine singleton