0

如何将音效与动画同步(CCAnimation)

        NSMutableArray* animationframes = [NSMutableArray array];
        [animationframes addObject:[[[CCAnimationFrame alloc] initWithSpriteFrame:[TitleLayer spriteframeForFile:@"title_startanime01.png"] delayUnits:1 userInfo:nil] autorelease]];
        [animationframes addObject:[[[CCAnimationFrame alloc] initWithSpriteFrame:[TitleLayer spriteframeForFile:@"title_startanime02.png"] delayUnits:1 userInfo:nil] autorelease]];
        [animationframes addObject:[[[CCAnimationFrame alloc] initWithSpriteFrame:[TitleLayer spriteframeForFile:@"title_startanime03.png"] delayUnits:1 userInfo:nil] autorelease]];
        [animationframes addObject:[[[CCAnimationFrame alloc] initWithSpriteFrame:[TitleLayer spriteframeForFile:@"title_startanime04.png"] delayUnits:1 userInfo:nil] autorelease]];
        [animationframes addObject:[[[CCAnimationFrame alloc] initWithSpriteFrame:[TitleLayer spriteframeForFile:@"title_startanime05.png"] delayUnits:1 userInfo:nil] autorelease]];
        [animationframes addObject:[[[CCAnimationFrame alloc] initWithSpriteFrame:[TitleLayer spriteframeForFile:@"title_startanime06.png"] delayUnits:1 userInfo:nil] autorelease]];
        CCAnimation* animation = [CCAnimation animationWithAnimationFrames:animationframes delayPerUnit:0.09 loops:1];

我可以以某种方式将调用块添加到动画帧数组吗?

或者,如果 CCAnimationFrame 在激活时具有可选的回调/委托,则它可以工作。

4

1 回答 1

0

好的,我们所要做的就是:

  1. 观察通知 CCAnimationFrameDisplayedNotification。它在动画的精灵上调用。

  2. 为了广播通知,需要将字典添加到要挂钩的 CCSpriteFrame 中。我添加了一个 NSDictionary,其中包含所有 spriteframes 的每个 sprite 的 spriteframename,因为我需要挂钩所有这些 spriteframe,但我猜字典也可能是空的,只是不是nil

        animationframe = [[[CCAnimationFrame alloc] initWithSpriteFrame:[cache spriteFrameByName:str] delayUnits:1 userInfo:nil] autorelease];
        animationframe.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:str, @"spriteframename", nil];
        [animationframes addObject:animationframe];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(frameupdatedinbootanimation:) name:CCAnimationFrameDisplayedNotification object:NULL];
    

然后抓住它

-(void)frameupdatedinbootanimation:(id)hmm {
    NSLog(@"frameupdatedinbootanimation: %@", hmm);
    Do something here
于 2013-10-07T03:52:38.387 回答