анимация движущихся спрайтов

#cocos2d-iphone

#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.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:

У CCFollow анимационный спрайт будет следовать за спрайтом мальчика:

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

Альтернативой является постоянное обновление положения анимационного спрайта до положения мальчика (sprite.position = boy.position) методом запланированного обновления.

Комментарии:

1. Спасибо за ваш ответ, но у мальчика-спрайта уже есть последовательность действий, одним из которых является эта анимация. итак, я сделал это: [действие спрайта: последовательность]; [действие спрайта: следовать]; и анимация возникает, но она следует в другом направлении, чем у моего мальчика — если она идет вправо, анимация идет влево .. и она также не в том же положении.. почему это?