Search code examples
iosobjective-csprite-kitskemitternode

SKEmitterNode targetNode path following is inverted


I created an SKEmitterNoder (standard Fire.sks template) & added it to an SKSpriteNode called ball. The thing is, I followed a tutorial of iOS Games by Tutorials & the way they taught me to create Tiled maps, is essentially flip them upon init so that (0, 0) would be in bottom left corner of the screen & NOT top left. I think this is affecting my SKEmitterNode targetNode property because as the ball is moving in the scene, the fire emitter node goes in the exact opposite direction. Could someone tell me what to do to change this? I can't change the world parameters, so I need something that would alter the SKEmitterNode trajectory so that it actually follows the ball node properly. Here's my current code:

NPCBall *ball   = [[NPCBall alloc] initWithBallType:BallTypeRed andName:@"Ball Red"];
ball.position   = CGPointMake(x + w/2, y + h/2);
ball.zPosition  = -104.0;
[_entityNode addChild:ball];

//Create a random Vector.dx & dy for the NPC. This will apply a random impulse to kick start NPC non-stop movement.
[ball.physicsBody applyImpulse:CGVectorMake([self randomVectorGeneration].dx, [self randomVectorGeneration].dy)];

//Create a particle for the ball (fire).
SKEmitterNode *emitFire = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"Particle_Fire" ofType:@"sks"]];
emitFire.position       = CGPointMake(ball.position.x, ball.position.y);
emitFire.targetNode = ball;
[_entityNode addChild:emitFire];

P.S. _entityNode, to which both the ball & SKEmitterNode fire are added, is an SKNode. Which in turn is a child of an SKNode called _worldNode. _worldNode is a child of self (which is an SKScene). This _worldNode is the one flipped to have (0, 0) coordinates begin at the bottom left.


Solution

  • A buddy of my'n has found a solution to this. I'm posting it here in case someone also has an inverted Tiled map where the (0, 0) coordinates are in the bottom left corner of the scene.

        //Create a ball sprite.
        SKSpriteNode *ball = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)];
        ball.position   = CGPointMake(_worldNode.frame.size.width/2, _worldNode.frame.size.height/2);
        [_entityNode addChild:ball]; //<--_entityNode is also an SKNode. It's a child of _worldNode.
        ball.zPosition  = -104.0;
    
        //Create a random Vector.dx & dy for the NPC. This will apply a random impulse to kick start NPC non-stop movement.
        [ball.physicsBody applyImpulse:CGVectorMake([self randomVectorGeneration].dx, [self randomVectorGeneration].dy)];
    
    
        //Create a particle effect for the ball.
        SKEmitterNode *emitFire = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"Particle_Fire" ofType:@"sks"]];
        emitFire.name           = @"Fire Emitter";
        emitFire.targetNode     = _worldNode; //<-- This is the demon. Make the emitter follow this SKNode since it moves when the ball moves. Everything in my game is a child of _worldNode.
        [ball addChild:emitFire]; //Add the emitter to the ball.