如何在cocos2d中设置动画的帧

时间:2011-10-02 22:48:30

标签: iphone animation cocos2d-iphone box2d

我制作了一个执行以下操作的CCAnimationHelper类。你给它一个fileName,一个frameCount和一个Delay,它会给你一个动画。我想知道的是如何设置动画的帧而不是从第1帧开始,它将从第10帧开始。这是我的代码

// Creates an animation from sprite frames.
+(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay
{
// load the ship's animation frames as textures and create a sprite frame
NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
for (int i = 1; i < frameCount; i++)
{
    NSString* file = [NSString stringWithFormat:@"%@%i.png", frame, i];
    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
    [frames addObject:frame];
}

// return an animation object from all the sprite animation frames
return [CCAnimation animationWithFrames:frames delay:delay]; //Is there another method I should be using here to set the frame
}

1 个答案:

答案 0 :(得分:1)

没有内置函数可以做到这一点,但您可以简单地创建动画并从您想要的帧开始传递NSArray:

    CCAnimation *animation = [CCAnimation animation];
    NSArray *offset = [NSArray arrayWithObjects:frame3, frame4, nil];
    [animation initWithFrames:offset];