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];
}
}
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With