2014-03-30 22 views
1

我正尝试在游戏开始之前预先加载我正在使用的图像。在cocos2d v3中预载图像

但是,我环顾四周,我只是找到这条线是写在cocos2d v2。

[[CCTextureCache sharedTextureCache ] addImage:@"objects.png" ]; 

如何预加载cocos2d v3中的图像?

回答

1

在cocos2d-v3中,您可以不使用CCTextureCache,而只需使用CCTexture(以前的CCTexture2D)textureWithFile:initializer。

你的代码是这样:

CCTexture* texture = [CCTexture textureWithFile:@"objects.png"]; 

如果我理解正确的文档,它会缓存纹理文件(见CCTexture extureWithFile:)。 因此,如果您之前已经加载了纹理,它将会在缓存中,并且之前创建的纹理将由上面的代码返回。

还有这里的另一个“变通”解决方案:https://gamedev.stackexchange.com/questions/72713/preload-in-cocos2d-v3

1

其实CCTextureCache仍然存在在cocos2d版本3.x但是如果你只导入“cocos2d.h”,它就不能直接访问。要访问它,你必须导入“CCTextureCache.h”。

试试这个代码:

#import "CCTextureCache.h" 

- (void) preloadImages 
{ 
    [[CCTextureCache sharedTextureCache] addImage:@"image1.png"]; 
    [[CCTextureCache sharedTextureCache] addImage:@"image2.png"]; 
    [[CCTextureCache sharedTextureCache] addImage:@"image3.png"]; 
    [[CCTextureCache sharedTextureCache] addImage:@"image4.png"]; 
    [[CCTextureCache sharedTextureCache] addImage:@"image5.png"]; 
} 
+1

看看到pmpod的答案。 [CCTexture textureWithFile:fileName]是正确的方式,因为它在引擎盖下调用[[CCTextureCache sharedTextureCache] addImage:fileName]。 – Lion

1

试试这个:

cocos2d::Director::getInstance()->getTextureCache()->addImage("objects.png");