I'm using SpriteKit and Xcode 6 to develop a game. I have a single sks file that I want to use in all game scenes. If I add the emitter into each scene after I present it, the emitter stops in the old scene and stars new in the new scene, because it is created in each scene.
Therefore I tried to use a singleton class for creating the emitter only once and using it in all game scenes. I created a super class for all game scenes and create the emitter in that super class for all child scenes. But, what I tried to get with this steps is not working. the emitter still stops and starts new in every new scene, because it still created new in each scene.
How can I use the same emitter bei creating it only once and use it in all my game scenes whithout stoping and new starting the emitter?
Here my code. What I'm missing?
P.S. The same technique is working well with an audio player! http://www.galloway.me.uk/tutorials/singleton-classes/
//MyEmitterNode.h
#import <SpriteKit/SpriteKit.h>
@interface MyEmitterNode : SKEmitterNode
+(instancetype)sharedEmitterNode;
- (void)addEmitterNodeIntoScene:(SKScene *)scene;
@end
//--
//MyEmitterNode.m
#import "MyEmitterNode.h"
@interface MyEmitterNode ()
@property (strong, nonatomic) SKEmitterNode *mEmitterNode;
@end
@implementation MyEmitterNode
+ (instancetype)sharedEmitterNode {
static MyEmitterNode *sharedEmitterNode = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedEmitterNode = [[self alloc] init];
});
return sharedEmitterNode;
}
- (void)addEmitterNodeIntoScene:(SKScene *)scene {
self.mEmitterNode = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"]];
[scene addChild:self.mEmitterNode];
}
@end
// -- MyScene: super classe for all game scenes
//MyScene.m
- (void)didMoveToView:(SKView *)view {
[[MyEmitterNode sharedEmitterNode] addEmitterNodeIntoScene:self];
}
// -- MyFirstScene: child class from MyScene
//MyFirstScene.m
- (void)didMoveToView:(SKView *)view {
[super didMoveToView:view];
// do other stuff
}
// -- MySecondScene: child class from MyScene
//MySecondScene.m
- (void)didMoveToView:(SKView *)view {
[super didMoveToView:view];
// do other stuff
}