【问题标题】:Animating Sprite Sheets in Cocos2dCocos2d 中的动画精灵表
【发布时间】:2014-05-11 15:10:49
【问题描述】:

我在为我的精灵表设置动画时遇到了一些问题。我之前得到了这个代码并且它有效。我有 2 个帧,我的循环中的变量只允许达到 0 和 1 的值,因为帧名称是stickman0-568.png 和stickman1-568.png。我的帧不太好,所以我制作了一个新的精灵表,里面有 3 个帧,stickman0-568.png、stickman1-568.png 和stickman2-568.png。我的 .list 文件名为ickman-568.plist,.png 精灵表名为stickman-568.png。我改变了循环,所以我只能达到 2(0,1,2) 的值。当我现在运行它时,它崩溃了,尽管它在我只有 2 帧之前工作。任何人都可以就可能出了什么问题给我建议吗?这是我的代码,所以你可以看到我做了什么:

-(id) init
{
    if( (self=[super init]) ) 
    {
        backGroundDesert = [CCSprite spriteWithFile:@"DesertMap.png"];
        backGroundDesert.position = ccp(backGroundDesert.textureRect.size.width/2, backGroundDesert.textureRect.size.height/2);
        [self addChild:backGroundDesert];
        [backGroundDesert runAction:[CCMoveBy actionWithDuration:100 position:ccp(-1400,0)]];

        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"stickman-568.plist"];
        CCSprite *mainGuy = [CCSprite spriteWithSpriteFrameName:@"stickman0-568.png"];
        mainGuy.position = ccp(40,backGroundDesert.textureRect.size.height/2);
        CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"stickman-568.png"];
        [batchNode addChild:mainGuy];
        [self addChild:batchNode];
        NSMutableArray *animFrames = [NSMutableArray array];
        for(int i = 0; i < 3; i++)
        {
            CCSpriteFrame *frames = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"stickman%02d-568.png", i]];
            [animFrames addObject:frames];
        }
        CCAnimation *cape = [CCAnimation animationWithSpriteFrames:animFrames delay:0.2f];
        [mainGuy runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:cape restoreOriginalFrame:NO]]];
    }
    return self;
}

-(void)dealloc
{
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [super dealloc];
}

【问题讨论】:

    标签: ios cocos2d-iphone


    【解决方案1】:

    当您运行代码时,for 循环会创建如下名称的图像

     stickman00-568.png
     stickman01-568.png
     stickman02-568.png
    

    这不是您源代码中的图像,因此只需在您的 for 循环代码中替换以下行。它工作正常。

    如下修改代码。

    CCSpriteFrame *frames = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"stickman%d-568.png", i]];
    

    See this

    【讨论】:

    • 感谢您的建议,但这不是问题所在。我意识到我的 .plist 文件的元数据部分中的 textureFileName 与我的代码不匹配。不过,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2011-11-05
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多