开发者

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 d开发者_如何转开发o the same as this line?

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


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


Andrew pretty much answered your question, I just want to give you a code segment about how to use CCTextureCache and CCSpriteFrameCache. Texture Cache load real textures/images and sprite frame cache loads the information regarding textures (if you are loading a Sprite sheet.) Okay here's the sample code.

Here latestBurp1-1.pvr.ccz and burpParticles1-1.png are my sprite sheets and their information is in (same name).plist files.

In the below function I am loading textures and also spriteFrames (the info about textures).

Also take a look at pvr files and pvr.ccz file they load much faster than png.

-(void) loadBurpAnimation {
        NSString* burpFile;
        NSString* burpFilePVR;

        NSString* burpParticlesFile;
        NSString* burpParticlesFilePVR;

        burpFile = @"latestBurp1-1.plist";
        burpFilePVR = @"latestBurp1-1.pvr.ccz";
        burpParticlesFile = @"burpParticles1-1.plist";
        burpParticlesFilePVR = @"burpParticles1-1.png";

        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:burpFile texture:[[CCTextureCache sharedTextureCache] addImage:burpFilePVR]];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:burpParticlesFile texture:[[CCTextureCache sharedTextureCache] addImage:burpParticlesFilePVR]];

        NSMutableArray *burpFrames = [NSMutableArray array];
        for(int i = 231; i <= 268; ++i) {
            [burpFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"burp.%d.png", i]]];
        }
        burpAnim = [[CCAnimation alloc] initWithFrames:burpFrames delay:0.04f];
        [burpFrames removeAllObjects];

        //Burp Particles
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:burpParticlesFile];
        NSMutableArray *burpParticlesFrames = [NSMutableArray array];
        for(int i = 3; i <= 37; ++i) {
            [burpParticlesFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Burp_%05d.png", i]]];
        }

        burpParticlesAnim = [[CCAnimation alloc] initWithFrames:burpParticlesFrames delay:0.04f];
        [burpParticlesFrames removeAllObjects];
}

I just gave you a lot of information so you might need to google some terms.


I think you are looking for [CCSpriteFrameCache sharedSpriteFrameCache]

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜