简体   繁体   中英

Preload Images in cocos2d v3

I'm trying to preload the images that I'm using before the game starts.

But, I looking around, I just find this line which is wrote in cocos2d v2.

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

How can I preload the images in cocos2d v3?

In cocos2d-v3, instead of using CCTextureCache, you can use simply CCTexture's (previously CCTexture2D) textureWithFile: initializer.

Your code would look like:

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

If I understand the docs correctly, it will cache the texture file (see CCTexture extureWithFile: ). So, if you already have loaded the texture before, it will be in cache, and the previously created texture will be returned by the code above.

There is another "work-around" solution also here: https://gamedev.stackexchange.com/questions/72713/preload-in-cocos2d-v3

Actually CCTextureCache is still there in Cocos2D version 3.x. But it is not directly accessible if you just import "cocos2d.h". To access it you have to import "CCTextureCache.h".

Try this code:

#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"];
}

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM