2

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
}
suyama
  • 262
  • 2
  • 15
  • 1
    This is just my point of view, but certainly not a solution...Your MyEmitterNode should subclass NSObject, not SKEmitterNode, because you don't use it like an emitter i.e. not adding it to the scene. Also dispatch_once_t is used when you want something to be executed once, and only once. You are creating new emitter node every time when addEmitterNodeIntoScene is called,(even MyEmitterNode is instantiated only once) and that is every time when MyScene's didMoveToView is called . And that is everytime when some of scenes are presented...So you get the picture... – Whirlwind Mar 20 '15 at 18:56
  • @Whirlwind You're right. I'm creating new emitter node every time when addEmitterNodeIntoScene is called. :-( I'm still searching for a solution. Thank you. – suyama Mar 21 '15 at 18:41
  • Nobody? It must surely be possible to add a node that stays available in all scenes without removing and creating it every time in every scene. There must be a technique that makes it possible. It works great with an audio player. Why should it not work with a node? No Idea? Thanks! – suyama Mar 30 '15 at 11:43

0 Answers0