SKAction playSoundFileNamed from Singleton [英] SKAction playSoundFileNamed from Singleton

查看:12
本文介绍了SKAction playSoundFileNamed from Singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个级别都使用相同的音效.我没有在每个级别都使用相同的代码,而是将所有声音合并到一个单例类中.但是,当我从其他类运行该方法时,将它放在一个单例中不会播放任何声音.我没有错误或警告.

I have several levels all using the same sound effects. Instead of having the same code in every level, I consolidated all of the sounds into a singleton class. However, having it in a singleton no sound is played when I run the method from other classes. I have no errors or warnings.

当我在每个类中使用相同的代码时,播放声音就没有问题了.

When I have the same code in each class I have no problems playing the sound.

问题: SKAction playSoundFileNamed 在从单例调用时不起作用还是我的代码缺少某些内容?

Question: Does SKAction playSoundFileNamed not work when called from a singleton or is my code missing something?

我的单例头文件...

-(void)soundSwordWhoosh;

我的单例方法文件...

My singleton methods file...

@implementation Animations{
    SKAction *swordWhooshSound;
}

-(id)init {
    self = [super init];
    if (self)
    {
        swordWhooshSound = [SKAction playSoundFileNamed:@"SwordWhoosh.mp3" waitForCompletion:YES];
    }
return self;
}

-(void)soundSwordWhoosh {
[self runAction:swordWhooshSound];
}

然后我这样调用方法:

[_animations soundSwordWhoosh];

推荐答案

您的单例可能不在节点层次结构中(即不是场景的子或孙等).因此,您不能在 self 上运行任何操作,因为单例实例不会收到来自 Sprite Kit 的定期更新.

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.

这是一个帮助类示例:

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

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

如果您担心缓存"该操作,则不值得这样做.你会做很多其他影响性能的事情.除了 Sprite Kit 在内部创建每个动作的副本之外,无论您是创建一个新的还是 Sprite Kit 的副本,它都不应该有太大的变化.如果你愿意,你仍然可以将它缓存在 static 变量中.

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.

你可以像这样从任何节点和任何方法调用辅助方法(不要忘记#import "SoundHelper.h"):

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:你不应该使用 .mp3 文件来制作音效,因为它非常低效.iOS硬件在硬件上一次只能解码一个mp3,其余的由CPU完成.更适合短音效的格式是 .caf 和 .wav.

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.

这篇关于SKAction playSoundFileNamed from Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆