在cocos2d v3中预加载图像

时间:2014-03-30 23:16:52

标签: ios objective-c cocos2d-iphone

我试图预先加载我在游戏开始前使用的图片。

但是,我环顾四周,我发现这行是用cocos2d v2写的。

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

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

3 个答案:

答案 0 :(得分:1)

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

您的代码如下:

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

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

还有另一个"解决方案"解决方案也在这里:https://gamedev.stackexchange.com/questions/72713/preload-in-cocos2d-v3

答案 1 :(得分: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"];
}

答案 2 :(得分:1)

试试这个:

 cocos2d::Director::getInstance()->getTextureCache()->addImage("objects.png");
相关问题