如何使用Cocos2d iPhone中的Sprite表?

时间:2012-09-29 22:05:39

标签: iphone animation cocos2d-iphone sprite-sheet

我有一位艺术家发给我的精灵表PNG。在精灵表内,有4个精灵,每个32x32,他们制作动画。我希望能够在屏幕上有一个CCSprite,他会完成动画。我尝试了各种各样的事情,但事实上,我真的只是不理解Sprite Sheets的概念。我正在网上看到需要财产清单等各种各样的东西。我从我的艺术家那里得到的只是一个简单的PNG。 请帮帮忙?

2 个答案:

答案 0 :(得分: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++;
}

答案 1 :(得分:0)

您可以使用Zwoptex工具导出带有cocos2D plist的spriteSheet。它的免费工具,易于使用。