【问题标题】:How do I work with a Sprite Sheet in Cocos2d iPhone?如何在 Cocos2d iPhone 中使用 Sprite Sheet?
【发布时间】:2012-09-21 08:04:50
【问题描述】:

我有一个艺术家发送给我的精灵表 PNG。在 sprite sheet 内,有 4 个 sprite,每个 32x32,它们制作动画。我希望能够在屏幕上显示一个 CCSprite,它会通过动画。我已经尝试了各种各样的东西,但说实话,我真的不明白 Sprite Sheets 的概念。我在网上看到了各种需要财产清单之类的东西。我从我的艺术家那里得到的只是一个简单的 PNG。 请帮忙?

【问题讨论】:

  • 属性列表只是所有帧的列表,以及它们在图像中的位置和大小。由于您没有该 plist,因此您需要手动指定每个矩形。我相信有一种方法可以让你指定这样的矩形。 [CCSprite spriteWithFile:rect:];要么这样,要么创建新的精灵帧并将它们添加到精灵帧缓存中。
  • 老实说,如果您能让艺术家将四张 32x32 图像作为单独的文件发送给您,您会轻松很多。然后下载TexturePacker之类的程序(基本功能不用付费)。这样,您就可以以最简单的方式轻松生成精灵表 PNG 和关联的 plist。
  • @Corbin87 如果您发布答案,如果您对我发表评论,我会支持它。我打算推荐 TexturePacker。

标签: iphone animation cocos2d-iphone sprite-sheet


【解决方案1】:

我也有这个问题,这是我想出的解决方案, 不需要 plist 也不需要 Zwoptex, 但这假设每一帧都是 32x32 并且以 32 个间隔完美对齐。

@interface GameLevelLayer() 
{
    CCSpriteBatchNode *trexSheet;
    int aniCount;
    NSTimer *autoWalker;


}
@end

在你的 init 或任何地方添加 spritesheet

trexSheet = [CCSpriteBatchNode batchNodeWithFile:@"dinosss.png"];
[self addChild:trexSheet];
aniCount=1;

使用 NSTimer 停止和启动动画

-(void)stopWalker{
        aniCount =1;
//set to start frame
    [self.player setDisplayFrame:
[CCSpriteFrame frameWithTexture:trexSheet.texture rect:
CGRectMake(0,0,32,32)]];

    if (autoWalker){   
    [autoWalker invalidate];
    autoWalker = nil;
    }

}

然后开始...

-(void)startWalker{

if (!autoWalker){ 
   [self walkimate];
    //Change the timeInterval to adjust animation speed
    autoWalker = [NSTimer scheduledTimerWithTimeInterval:0.15
                                             target:self
                                           selector:@selector(walkimate)
                                           userInfo:nil
                                            repeats:YES];
} 
}

循环函数

-(void)walkimate{
    ///loop through animation
if (aniCount>4) {
    ///set to 0 because we add one at the end before the next call
    aniCount =0;
}

//this finds the 32x32 pa
[self.player setDisplayFrame:
[CCSpriteFrame frameWithTexture:trexSheet.texture rect:
CGRectMake(32*i,0,32,32)];

aniCount++;
}

【讨论】:

    【解决方案2】:

    您可以使用 Zwoptex 工具将 spriteSheet 与 cocos2D plist 一起导出。它的免费工具且易于使用。

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2013-11-16
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多