【问题标题】:Syncing sounds with frames inside CCAnimation for cocos2d 2.x*在 cocos2d 2.x 的 CCAnimation 中将声音与帧同步 *
【发布时间】:2013-10-07 03:17:04
【问题描述】:

如何将音效与动画同步 (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 在激活时有一个可选的回调/委托,它也可以工作。

【问题讨论】:

标签: animation cocos2d-iphone synchronization audio


【解决方案1】:

好吧,我们要做的就是:

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

  2. 为了 要广播的通知,需要添加字典 到您要挂接的 CCSpriteFrame。我添加了一个 NSDictionary 包含所有 spriteframes 的每个 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];
    

然后抓住它

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

【讨论】:

  • 根据文档。 ymmv :我在游戏的一个关键方面进行了尝试(例如,在攻击者动画帧 9 上同步一个 fx)。特别是在模拟器中,FPS 是一个随机数,并且通常不会发布通知。我推断这是因为实际上跳过了帧......所以我最终离开了它。
  • 有文档(源 cmets 除外)?无论如何,我也使用这种方法来调整精灵位置,因为我的设计师给我发送了每一帧的不同来源。呃。所以我最终在那里制作了一个小型 API(dic 中的 pos 键意味着原点更改),因为每个通知只能有一个处理程序。让我希望有一天也有人会将其纳入 CCAction 流程。
  • 大声笑,这只是对文档的“讽刺”。 Notin, nada, null, rien !无论如何,如果您所做的只是调整每帧的某些内容(位置、透明度等),并且需要做的事情的性质在字典中(ergo api),那么它将为您工作。如果你需要做一些对游戏至关重要的事情(就像我做的那样),请避免这样做。
猜你喜欢
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
相关资源
最近更新 更多