анимация движущегося спрайта - PullRequest
1 голос
/ 11 ноября 2011

У меня есть мир 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 голосов
/ 11 ноября 2011

CCFollow будет иметь спрайт анимации, следующий за спрайтом мальчика:

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

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...