iosswiftsprite-kitskemitternode

How to make SKEmitter particles stay relative to the background when I move my SKEmitterNode around the screen?


I feel like this should be a very simple question, but I looked around and it seems to just be working for everybody automatically. But for some reason, when I move the particle emitter around the screen, the particles move with it, and there is no trail behind the emitter node of particles.

I am making a SpriteKit game of a character running around on the bottom of the screen, and he has a boost ability which releases small bubble particles behind him when he boosts because he is underwater. But the particles aren't staying relative to the screen, they are relative to the player and move with him.

Here's some code to add the boost bubbles:

func addBoostBubbles(){
    boostBubbles = SKEmitterNode(fileNamed: "bubbleBoost")!
    boostBubbles.particlePositionRange = CGVector(dx: player.frame.size.width, dy: player.frame.size.height)
    boostBubbles.position = player.position
    boostBubbles.zPosition = 0
    self.addChild(boostBubbles)
}

I call this func to create the emitter node and set it to the player's position.

then to move the position of the bubbles in the didSimulatePhysics func I have this:

boostBubbles.position.x += xAcceleration

I see the bubbles on the screen and the emitter node is moving to the right places, but I want them relative to the background so the bubbles just slowly float up behind the player.

Any help would be greatly appreciated!!!!


Solution

  • Set the targetNode property of your emitter to be the background node. In addBoostBubbles, try adding the line:

    boostBubbles.targetNode = <background node name> 
    

    https://developer.apple.com/documentation/spritekit/skemitternode

    targetNode - The target node that renders the emitter’s particles.

    This causes the particles to render as though they are children of the background.

    If you don't have a background node then you'll need to add one.