sprite kit - SKAction playSoundFileNamed from Singleton -
i have several levels using same sound effects. instead of having same code in every level, consolidated of sounds singleton class. however, having in singleton no sound played when run method other classes. have no errors or warnings.
when have same code in each class have no problems playing sound.
question: skaction playsoundfilenamed
not work when called singleton or code missing something?
my singleton header file...
-(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]; }
i call method this:
[_animations soundswordwhoosh];
your singleton not in node hierarchy (ie not child or grandchild etc of scene). hence can't run actions on self
singleton instance not receive regular updates sprite kit.
it not necessary use singleton here because can same thing easier using class methods. need pass in node sound should played for.
here's 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 worried "caching" action it's not worth doing it. you'll plenty of other things affect performance far more. besides sprite kit internally creates copy of every action, whether create new 1 or sprite kit copies shouldn't change much. still cache in static
variables if wanted to.
you can call helper methods node , method (don't forget #import "soundhelper.h"
):
-(void) somenodemethod { [soundhelper playswordsoundwithnode:self]; }
ps: should not use .mp3 files sound effects, highly inefficient. ios hardware can decode 1 mp3 @ time in hardware, rest done cpu. better suited formats short sound effects .caf , .wav.
Comments
Post a Comment