TextureMgr发生了什么?

时间:2011-01-14 00:00:09

标签: image cocos2d-iphone load textures

在cocos2d中,曾经有一个TextureMgr方法(Async),可以让你加载图像供以后使用。现在,当我使用TextureMgr时,它表示它未声明。它被弃用了吗?我在.99.5。如果它不再可用,取而代之的是什么?有什么能和这条线一样吗?

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

3 个答案:

答案 0 :(得分:2)

看看CCTextureCache

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

也许是你在寻找。

当您创建任何纹理对象时使用此缓存:例如sprite。您可以使用它来预先处理图像。

修改 正如我所说的那样,当您使用纹理创建ang对象时,会使用CCTextureCache - 因此,如果纹理已经在缓存中,则元素创建要快得多,如果您先加载纹理然后再创建对象。

例如,如果您正在编写如下代码:

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

并且@“my-file.png”纹理不在缓存中,它将首先加载,并且需要一些时间(更多的是创建一个对象)。

如果你正在编写这样的代码:

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

然后sprite1将被创建得慢,sprite2会更快,因为纹理已经在缓存中。

您可以手动添加纹理到缓存

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

并且使用此纹理创建所有对象将很快。

当您需要预先处理纹理时,代码中的常见位置是游戏加载或级别包加载或级别加载。

如果您需要使用SimpleAudioEngine singleton,您可以预先缓解声音

答案 1 :(得分:1)

Andrew几乎回答了你的问题,我只想给你一个关于如何使用CCTextureCache和CCSpriteFrameCache的代码段。纹理缓存加载真实纹理/图像和精灵帧缓存加载有关纹理的信息(如果您正在加载一个Sprite表。) 好的,这是示例代码。

这里的latestBurp1-1.pvr.ccz和burpParticles1-1.png是我的精灵表,他们的信息是(同名).plist文件。

在下面的函数中,我正在加载纹理以及spriteFrames(有关纹理的信息)。

另外看看pvr文件和pvr.ccz文件,它们的加载速度比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];
}

我刚刚给了你很多信息,所以你可能需要谷歌一些术语。

答案 2 :(得分:0)

我认为您正在寻找[CCSpriteFrameCache sharedSpriteFrameCache]

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