同步里面CCAnimation帧进行了cocos2d 2.x的声音*(Syncing sounds

2019-10-29 16:40发布

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

我能以某种方式加入callblock到animationframes阵列?

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

Answer 1:

好了,我们所要做的是:

  1. 观察通知CCAnimationFrameDisplayedNotification。 这就是所谓的上是动画精灵。

  2. 为了使通知被广播,字典将需要添加到您要挂接到CCSpriteFrame。 我加入包含每个精灵的spriteframename所有spriteframes,因为我需要挂钩所有的人一个的NSDictionary,但我想词典可能是空的为好,只是不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


文章来源: Syncing sounds with frames inside CCAnimation for cocos2d 2.x*