将声音与CCAnimation中的帧同步,用于cocos2d 2.x *

时间:2013-10-07 03:17:05

标签: animation cocos2d-iphone synchronization audio

如何将声音效果与动画同步(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];

我可以以某种方式向animationframes数组添加一个callblock吗?

如果CCAnimationFrame在激活时有一个可选的回调/委托,它可以工作。

1 个答案:

答案 0 :(得分:0)

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

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

  2. 为了 要播放的通知,需要添加字典 到你要挂钩的CCSpriteFrame。我添加了一个NSDictionary 包含所有spriteframe的每个sprite的spriteframename 因为我需要挂钩所有这些,但我想字典可以 也是空的,不是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];
    
  3. 然后抓住它

    -(void)frameupdatedinbootanimation:(id)hmm {
        NSLog(@"frameupdatedinbootanimation: %@", hmm);
        Do something here