移动精灵动画

时间:2011-11-11 17:28:07

标签: cocos2d-iphone

我有一个cocos2d世界,一个快速移动的精灵/身体。 当发生接触时我正在调用动画功能。 问题是,当动画在当前精灵位置运行时,精灵已经去了另一个地方,所以动画不在正确的位置:

我如何运行此动画功能来跟踪我的精灵?

代码:

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

    NSLog(@"check:%@",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=boy.position;
    //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];
    }


    //CCAnimation *dollAnimation = [CCAnimation animation];
    CCAnimation* dollAnimation = [CCAnimation animationWithFrames:animFrames delay:0.1f];
    //CCAnimation *dollAnimation = [CCAnimation animationWithName:@"dance" animationWithFrames:animFrames];
    //[dollAnimation setDelay:0.1f];
    CCAnimate * 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)

CCFollow将动画精灵跟随男孩精灵:

id follow = [CCFollow actionWithTarget:boy];
[sprite runAction:follow];

替代方法是在计划更新方法中将动画精灵的位置连续更新为男孩位置(sprite.position = boy.position)。