Sound synchronization with frames inside CCAnimation for cocos2d 2.x *

How to synchronize sound effects with animation (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]; 

Is there any way to add a call block to an array of animation frames?

Or it may work if CCAnimationFrame has an optional callback / delegate to activate.

0
source share
1 answer

Ok, all we need to do:

  • Observe the CCAnimationFrameDisplayedNotification notification. He called for the sprite animation aspect.

  • In order for the notification to be broadcast, you must add the dictionary to the CCSpriteFrame to which you want to connect. I added an NSDictionary containing the spriteframename of each sprite for all sprite frames since I need to hook them all, but I think that the dictionary could also be empty, not 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]; 

Then catch it

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

All Articles