0

I am setting up an SKNode as follows:

- (id)init { self = [super init]; if (self) { SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"Sprites"]; SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithTexture:[atlas textureNamed:@"Cat"]]; [self addChild:spriteNode]; // Create particle trail SKEmitterNode *emitterNode = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"Hearts" ofType:@"sks"]]; emitterNode.position = CGPointMake(0, 20); [self insertChild:emitterNode atIndex:0]; // Setting up the physics self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.frame.size]; self.physicsBody.dynamic = YES; self.physicsBody.affectedByGravity = NO; } return self; } 

When this node moves around horizontally along the X-axis, the source of the particle trail moves along with it, which is expected. What is not expected is that emitted particles do this as well, when instead I expect them to move straight up along the Y-axis from their original X position.

Is there a way to prevent this behaviour?

Creation (in scene):

SPAPlayer *player = [[SPAPlayer alloc] init]; player.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)); [self addChild:player]; 

Movement code:

[node.physicsBody applyForce:CGVectorMake(20 * data.acceleration.x, 0)]; 

Image for reference:

enter image description here

1
  • Have you tried modifying the acceleration of the emitter by the inverse of your sprite's velocity? (or similar, I'm terrible with these terms) Commented Jun 19, 2014 at 12:39

1 Answer 1

2

Make the emitter a child of the sprite (as you already have).

To make the particles move independently from where the player node is, assign the SKEmitterNode's targetNode property, preferably assign it the scene as in:

// be sure self.scene isn't still nil when you do this, for instance in node's init emitter.targetNode = self.scene; 

This will spawn the particles at the player sprite position but they won't travel along with it.

Sign up to request clarification or add additional context in comments.

4 Comments

Setting targetNode to self doesn't seem to change anything. Doing [self.scene addChild:emitterNode] Makes the emitter disappear entirely. Added a reference image in case my problem formulation was unclear.
Oh sorry, I think I got that mixed up. You want to keep the particle emitter as a child of the sprite, then set the targetNode to the scene. That will make the particles spawn at the player sprite position, but particles move relative to the scene (which doesn't move so they move freely and are not bound by the sprite's motion). Answer updated.
It does indeed seem like self.scene is nil at the point in time in which I set targetNode. Not sure how to get around this at the moment. Marking your answer nevertheless as it seems accurate. Updating question with information on where I add the sprite in question in case it can help further.
This answer was helpful for me. Note, you can use self.parent if your node's parent is the scene.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.