iossprite-kitskemitternodezposition

How to move emitter node to front?


Background's zPosition is 1 and emitter node's is 2. Still background is on front. I even tried higher numbers, but it won't help.

Code

SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:@"TiltToMove_BG"];
bg.size = self.size;
bg.zPosition = 1;
bg.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5);
[self addChild:bg];

NSString *fireEmmitterPath = [[NSBundle mainBundle] pathForResource:@"fire" ofType:@"sks"];
SKEmitterNode *fireEmmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:fireEmmitterPath];
fireEmmitter.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5 - 100);
fireEmmitter.name = @"fireEmmitter";
fireEmmitter.zPosition = 2;
fireEmmitter.targetNode = self;
[self addChild:fireEmmitter];

Solution

  • There is a property of SKEmitterNode called particleZPosition.

    The default value is 0.0 so you will need to change this to make the particles appear in front.

    fireEmitter.particleZPosition = 2;
    

    The zPosition of the actual emitter node is largely irrelevant as it is invisible.

    This is because you are putting the particles onto the parent node...

    fireEmitter.targetNode = self;
    

    The target node is where the particles are added.

    If you had left that as nil then the particles would be added to the emitter node and so would already be in front of the background because they would be at zPosition 0.0 on the emitter node which has zPosition 2.0.

    Of course, if you then move the emitter around, all the particles would then move in the same way as the emitter so maybe thats not the effect you're after.

    Anyway, you should be sorted now.

    You can find this information in the documentation of SKEmitterNode.