【问题标题】:Cocos2d multipack sprite sheet animation example - How to load a multipack sprite sheet?Cocos2d 多包精灵表动画示例 - 如何加载多包精灵表?
【发布时间】:2013-10-18 00:29:03
【问题描述】:

我是 Cocos2d 的新手,过去 3 天一直在尝试加载多包精灵表动画。

我找不到教程或工作示例,查看了raywenderlich.com,发现这篇讨论单文件精灵表动画的帖子:Example 1

这个Example 2 也不行:

那么如何加载多包精灵表?

谢谢

我添加了部分有效的代码。问题是: 1) 文件 -ipadhd 和 -hd 未按预期识别(在其他动画中它们被识别,因此 AppDelegate 中的设置正常) 2) 加载多包动画时出现异常 文件示例: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];

        }

【问题讨论】:

  • 你的问题是什么?
  • 我的问题是如何加载多包精灵表?
  • 什么是“multipack”精灵表?我从来没有听说过这个词。如果您的意思是两个或多个精灵表,您只需一个接一个地加载,但您将不得不在每张表(图集)中使用一个批处理节点。
  • 我刚刚添加了我的代码。请帮忙:)

标签: ios objective-c animation cocos2d-iphone


【解决方案1】:

我正在使用 js 绑定在 cocos2d-html5 和 cocos2dx 中工作,但是 - 我的回答应该仍然适用。据我所知,cocos2d 中没有对 multipack 的特殊处理,这是 Texture Packer 中可用的构造。您应该加载所有 plist + png,就好像它们是单个节点一样。从那里,您应该能够从它们中的任何一个创建精灵和动画,因为它们都将在缓存中。

代码:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多