Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a trail with SKEmitterNode and particles in SpriteKit

I am trying to make it so a particle I made follows the player whenever the player is moved. The effect I am trying to replicate is like when you are on a website and they have some sort of set of objects following your mouse. I tried to do this by making the particle move by the amount the player does, but it is not reproducing the intended effect. Any suggestions? My code:

Declaring particle

NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"trail" ofType:@"sks"];
self.trailParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
self.trailParticle.position = CGPointMake(0,0);
[self.player addChild:self.trailParticle];

Move method

 -(void)dragPlayer: (UIPanGestureRecognizer *)gesture {

         if (gesture.state == UIGestureRecognizerStateChanged) {

              //Get the (x,y) translation coordinate
              CGPoint translation = [gesture translationInView:self.view];

              //Move by -y because moving positive is right and down, we want right and up
              //so that we can match the user's drag location (SKView rectangle y is opp UIView)
              CGPoint newLocation = CGPointMake(self.player.position.x + translation.x, self.player.position.y - translation.y);
              CGPoint newLocPart = CGPointMake(self.trailParticle.position.x + translation.x, self.trailParticle.position.y - translation.y);

              //Check if location is in bounds of screen
              self.player.position = [self checkBounds:newLocation];
              self.trailParticle.position = [self checkBounds:newLocPart];
              self.trailParticle.particleAction = [SKAction moveByX:translation.x y:-translation.y duration:0];
              //Reset the translation point to the origin so that translation does not accumulate
              [gesture setTranslation:CGPointZero inView:self.view];

         }

    }
like image 226
EvilAegis Avatar asked Dec 21 '13 19:12

EvilAegis


1 Answers

Try this:

1) If your emitter is in the Scene, use this emitter's property targetNode and set is as Scene. That means that particles will not be a child of emitter but your scene which should leave a trail..

Not sure if this is correct (I do it in C#):

self.trailParticle.targetNode = self; // self as Scene

And some extra:

2) I think you could rather attach your emitter to self.player as child so it would move together and automatically with your player and then there is no need of this:

self.trailParticle.position = [self checkBounds:newLocPart];
self.trailParticle.particleAction = [SKAction moveByX:translation.x y:-translation.y duration:0];
like image 140
Vaclav Elias Avatar answered Oct 14 '22 08:10

Vaclav Elias