SKAction playSoundFileNamed from Singleton

流过昼夜 提交于 2019-12-01 11:02:59

Your singleton is probably not in the node hierarchy (ie not a child or grandchild etc of the scene). Hence you can't run any actions on self as the singleton instance will not receive regular updates from Sprite Kit.

It is also not necessary to use a singleton here because you can do the same thing easier using class methods. You just need to pass in the node that the sound should be played for.

Here's an example helper class:

@interface SoundHelper
+(void) playSwordSoundWithNode:(SKNode*)node;
@end

@implementation SoundHelper
+(void) playSwordSoundWithNode:(SKNode*)node
{
    [node runAction:[SKAction playSoundFileNamed:@"SwordWhoosh.mp3" waitForCompletion:YES]];
}
@end

If you are worried about "caching" the action it's not worth doing it. You'll do plenty of other things that affect performance far more. Besides Sprite Kit internally creates a copy of every action, whether you create a new one or Sprite Kit copies it shouldn't change much. Still you could cache it in static variables if you wanted to.

You can call the helper methods like so from any node and any method (don't forget to #import "SoundHelper.h"):

-(void) someNodeMethod
{
    [SoundHelper playSwordSoundWithNode:self];
}

PS: you should not use .mp3 files for sound effects, as it is highly inefficient. The iOS hardware can only decode one mp3 at a time in hardware, the rest is done by the CPU. Better suited formats for short sound effects are .caf and .wav.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!