0

我正面临着我正在开发的游戏的问题。我有几个动画,我的问题是,它们工作正常,但第一次加载时,有延迟,只是第一次。我不知道如何解决它。我已经阅读了所有关于 CCAnimationCache 的文档,我已经阅读了论坛并且我没有成功。我仍然遇到同样的问题,动画可以工作,但是第一次加载其中一个时,会有延迟。你能帮我吗?我也读过这个网站:http ://www.johnwordsworth.com/2011/07/loading-cocos2d-sprite-frame-animations-from-plist-files/他们谈到预加载,但我的结果是一样的。

我在一开始就缓存了动画,它们似乎在缓存中,但结果并不是一直都想要,而且每次我第一次加载其中一个动画时,我都会遇到这种延迟。

我编写了一个函数来按照规范预加载所有动画并使用 CCAnimationCache 并且它不起作用或者我可能错了。这是我制作的一些代码:

for(int element = 0; element < 3; element++){
        for(int pos = 0; pos < 3; pos++){
 CCAnimation *auxAnimation = nil;

            animation =  getAnimationInformationWithElement(element, pos);
            [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"%@.plist",animation.animationFileHeader]];
            for(int i = 0; i <= animation.numberOfFrames; i++) {
                if(i<=9)auxFileName = [animation.animationPNGFileName stringByAppendingString:@"0000"];
                else auxFileName = [animation.animationPNGFileName stringByAppendingString:@"000"];
                [elementsAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@%d.png",auxFileName,i]]];
            }

            auxAnimation = [CCAnimation elementsAnimFrames];

            [animationsToBeSaved elementsAnimFrames];
            elementsAnimFrames = [[[NSMutableArray alloc] init] autorelease];

            [[CCAnimationCache sharedAnimationCache] addAnimation:auxAnimation name:[NSString stringWithFormat:@"animation_%d_%d",element,pos]];
        }
    }

有任何想法吗?

4

1 回答 1

0

假设您的动画是 .png Spritesheets,我建议您将图像异步预加载到缓存中。

/** 异步,从文件中加载一个texture2d。* 如果文件图像先前已加载,它将使用它。* 否则它将在新线程中加载纹理,并且当加载图像时,将调用块。* 回调将在 cocos2d 线程中调用,因此从回调创建任何 cocos2d 对象都是安全的。* 支持的图片扩展名:.png、.bmp、.tiff、.jpeg、.pvr、.gif * @since v2.0 */

-(void) addImageAsync:(NSString*) filename withBlock:(void(^)(CCTexture2D *tex))block;

例如,按如下方式使用它:

[[CCTextureCache sharedTextureCache] addImageAsync:@"spritesheetName.png" withBlock:^(CCTexture2D *texture){
        @synchronized(self){
            if (texture)
                //increment preloaded assets count or whatever..
        }
    }];
于 2013-09-24T03:05:41.717 回答