1
votes

I am new to Cocos2d and in the past 3 days have been trying to load a multipack sprite sheet animation.

I was unable to find a tutorial or a working example and looked at raywenderlich.com and found this post which discusses single file sprite sheet animation: Example 1

This Example 2 does not work as well:

So how to load a multipack sprite sheet?

Thanks

I added my code which works partially. The problems are: 1) The files -ipadhd and -hd are not recognised as expected (in other animations they are recognised so the settings in AppDelegate is Ok) 2) When multipack animation is loaded I get an Exception File example:apple0.png, apple0.plist, apple0-hd.png ,apple0-hd.plist, apple1-hd.png, apple1-hd.plist, apple0-ipadhd.png, apple0-ipadhd.plist, apple1-ipadhd.png, apple1-ipadhd.plist

        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL exists = YES;
        int plistIndex = -1;
        int tmpPlistIndex;
        NSMutableArray *availablePLists = [NSMutableArray array];

        NSMutableArray *imageFrames = [NSMutableArray array];
        NSMutableArray *spriteSheets = [NSMutableArray array];
        frameCount = 0;

        do {
            plistIndex +=1;
            NSString* plistFileName = [NSString stringWithFormat:@"%d%@",plistIndex,item.fileName];

            plistFileName = [[NSBundle mainBundle] pathForResource:plistFileName ofType:@"plist"];



            plistPath = plistFileName;
            [availablePLists addObject:plistPath];
            plistDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
            frames = [plistDict objectForKey:@"frames"];
            frameCount += frames.count;

            tmpPlistIndex = plistIndex + 1;
            plistFileName = [NSString stringWithFormat:@"%d%@",tmpPlistIndex,item.fileName];
            plistFileName = [[NSBundle mainBundle] pathForResource:plistFileName ofType:@"plist"];

            exists = [fileManager fileExistsAtPath:plistFileName];

        } while (exists);


        NSLog(@"frame count is: %d",frameCount);

        plistIndex = -1;
        exists = YES;

        for(NSString *availablePList in availablePLists)
        {
            plistIndex += 1;


            plistDict = [[NSDictionary alloc] initWithContentsOfFile:availablePList];

            NSLog(@"PLIST FILE NAME = %@",availablePList );

            frames = [plistDict objectForKey:@"frames"];
            [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:availablePList];
            NSString* imageFileName = [NSString stringWithFormat:@"%d%@.png",plistIndex,item.fileName];
            //  NSDictionary* metadata = [plistDict objectForKey:@"metadata"];
            // NSString* imageFileName = [metadata objectForKey:@"realTextureFileName"];

            CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:imageFileName];

            [self addChild:spriteSheet z:100];
            item.spriteSheet = spriteSheet;

            [spriteSheets addObject:spriteSheet];

            for (int i=0; i<frameCount; i++) {
                NSString * fileToLoad;

                if(i<10){
                    fileToLoad = [NSString stringWithFormat:@"%@/000%d",item.fileName,i];
                }
                else if (i<100){
                    fileToLoad = [NSString stringWithFormat:@"%@/00%d",item.fileName,i];
                }
                else{
                    fileToLoad = [NSString stringWithFormat:@"%@/0%d",item.fileName,i];
                }


                if ([frames objectForKey:fileToLoad]){
                    NSLog(@"Found : %@",fileToLoad);
                    [imageFrames addObject:
                     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileToLoad]];
                }

            }


        }//End available plists



        item.spriteAnimation = [CCAnimation animationWithSpriteFrames:imageFrames delay:0.1f];
        NSString* spriteImageName = [NSString stringWithFormat:@"%@/0000",item.fileName];
        NSLog(@"IMAGE LOADING spriteImageName : %@",spriteImageName);
        item.sprite = [CCSprite spriteWithSpriteFrameName:spriteImageName];
        NSLog(@"IMAGE LOADED spriteImageName : %@",spriteImageName);
        CGPoint point = item.itemInfo.position;//info.position;
        CGPoint pointScaled = [self scalePoint:point];
        item.sprite.position =  pointScaled;
        NSLog(@"POS name-%@ pos (%f, %f)",item.name,item.sprite.position.x,item.sprite.position.y);
        item.sprite.anchorPoint=ccp(0, 0);

        for(CCSpriteBatchNode* spriteSheet in spriteSheets){

            //[spriteSheet addChild:item.sprite];
            [self  addChild:item.sprite];

        }
1
and what is your question?Kreiri
My question is how to load a multipack sprite sheet?Kety
what is a "multipack" sprite sheet? I have never heard that term. If you mean two or more spritesheets you simply load one after the other but you're going to have to use one batch node per sheet (atlas).LearnCocos2D
I just added my code. Please help :)Kety

1 Answers

0
votes

I'm working in cocos2d-html5 and cocos2dx with js bindings but - my answer should still apply. There is no special handling of multipack within cocos2d as far as I am aware, this is a construct available within Texture packer. You should load all of your plists + pngs as if they were individual nodes. From there, you should be able to create sprites and animations from any of them as they'll all be in the cache.

Code:

jc.makeSpriteWithMultipackPlist = function(plists, pngs, startFrame){
    var sprite = new cc.Sprite();
    for(var i =0;i<plists.length;i++){
        var plist = plists[i];
        var png = pngs[i];
        if (!jc.parsed[plist]){
            cc.SpriteFrameCache.getInstance().addSpriteFrames(plist);
            cc.SpriteBatchNode.create(png);
            jc.parsed[plist]=true;
        }
    }

    var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(startFrame);
    if (!frame){
        throw "Frame: " + startFrame +  " not in cache.";
    }
    sprite.initWithSpriteFrame(frame);
    sprite.retain();
    return sprite;
}