سؤال

I have a lot of images that I want to preload and use in a sprite animation:

I'm preloading the images like this:

NSArray *imageHeroMenus = @[
                            @"hero_menu0001.png", //0
                            @"hero_menu0002.png", //0
                           ...
                            @"hero_menu0120.png", //0
                            ];

for (NSString *imageHero in imageHeroMenus)
    {


        CCTexture * tex = [CCTexture textureWithFile:imageHero];
        [loadedHeroMenu addObject:tex];


    }

How can I use this imageHeroMenus to create an animated sprite?

Because when I load a static sprite, I use this code:

background3 = [CCSprite spriteWithTexture:[loadedTextures objectAtIndex:0]];

With objectAtIndex at the index that I want of the array. But I don't know how to adapt it to a animated sprite.

هل كانت مفيدة؟

المحلول

I understand you use cocos2d v3.0? If yes - try to use fore example a solution presented here: Sprite Frame Animation Cocos2d 3.0. Hope it helps - i think the approach by using SpriteFrames described in the link is a good one. Also - it would be easier to name your files in way:

 "hero_menu1.png, hero_menu2.png,... hero_menu11.png,..." 

rather than:

"hero_menu0001.png, hero_menu0002.png,... hero_menu0011.png,..."

since then in code you can load all files in a simple for: NSMutableArray *animFrames = [NSMutableArray array]; for(int i = 1; i <= 120; i++) { CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"hero_menu%d.png",i]]; [animFrames addObject:frame]; }

and the use them in CCAnimation (see the link above).


If you want to use CCTexture You can use

(void) addSpriteFrameWithTexture:(CCTexture*)texture rect:(CGRect)rect;

method (see CCAnimation.h) to add animation frames with your uploaded textures (for example in a for loop, instead of using animationWithSpriteFrames explained in the example I mention before.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top