2011-01-14 140 views
2

在cocos2d中,曾经有一种TextureMgr方法(异步),可以让您加载图像供以后使用。现在当我使用TextureMgr时,它说它是未声明的。它是否被弃用?我在.99.5上。如果它不再可用,什么取代它?有什么可以做到这一行相同?TextureMgr发生了什么?

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

回答

2

看看CCTextureCache

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

也许这就是你所期待的。

这个缓存是用来创建任何对象的纹理:例如精灵。您可以使用它来预览图像。

编辑: 的CCTextureCache当纹理您创建昂的对象,因为我说的是使用 - 所以如果纹理已经在高速缓存中的元素创建得多快然后如果你会先加载纹理然后创建一个对象。

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

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

和@“我-file.png”纹理不在缓存中就将其装入第一,这将需要一些时间(更多的只是创建一个对象)。

如果你像这样写代码:

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

然后sprite1将创建缓慢,sprite2更多更快,因为质地已经在缓存中。

您可以MANUALE添加纹理缓存

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

与此纹理的所有对象的创建将会很快。

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

此外你是否需要使用它们SimpleAudioEngine单

+0

谢谢安德鲁!你还回答了我的另一个问题,哈哈,但你能提供更多的信息吗?谢谢;) – Joethemonkey101 2011-01-14 20:53:35

1

安德鲁几乎回答了你的问题,可以预先缓存的声音,我只是想给大家介绍如何使用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]; 
} 

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