为移动的精灵制作动画

时间:2011-10-16 14:39:27

标签: cocos2d-iphone

我知道如何使用cocos2d使用spritesheet和plist为精灵设置动画。

我有一个世界,当施加力量时,精灵可以在这个世界中移动。

我需要动画那个精灵,在移动时,让我说他跌倒了,所以他的眼睛应该在他跌倒时眨眼,或者移动等等。

我现在动画的代码是这样的,当我插入一个新的精灵,但我需要将它应用于特定的精灵,而不是在特定的地方......

-(void)animation:(NSString *)animation
{


    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"%@.plist",animation]];
    sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%@_00000.png",animation]]; //take the corrdinates of this picture from the plist

    //sprite.position=ccp(240,160);
    //sprite.position=ccp(160,175);

    CCSpriteBatchNode *spriteSheet = [ CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"%@.png",animation]];
    [spriteSheet addChild:sprite]; //add this coordinates from the spritesheet to the screen
    [self addChild:spriteSheet];


    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *animPath = [Path stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.plist", animation]];
    NSDictionary *animSpriteCoords = [[NSDictionary alloc] initWithContentsOfFile: animPath];
    NSDictionary *animFramesData = [animSpriteCoords objectForKey:@"frames"];
    int b=0;
    int a=0;
    NSMutableArray *animFrames = [NSMutableArray array];
    for(int i = 1; i < [animFramesData count]; i++) 

    {
        a=a+1;
        if(a==10)
        {
            b=b+1;
            a=0;
        }

        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@_000%0i%1i.png",animation,b,a]];   //[NSString stringWithFormat:@"eye_blinking_0000%1d.png",i]
        [animFrames addObject:frame];
    }

    CCAnimate *Action ;
    CCAnimation* dollAnimation = [CCAnimation animationWithFrames:animFrames delay:0.1f];
    Action = [CCAnimate actionWithAnimation:dollAnimation];
    id call=[CCCallFunc actionWithTarget:self selector:@selector(finishAnimation)];
    id sequence=[CCSequence actions:Action,[CCHide action],call,nil];
    [sprite runAction:sequence];

}

1 个答案:

答案 0 :(得分:0)

你必须改变动画开始移动时走动,并在[sprite runAction:sequence]中停止时改变;