使用SKEmitterNode和SpriteKit中的粒子创建一个轨迹

8

我正在尝试让我创建的粒子在玩家移动时跟随玩家。我想要复制的效果就像当你在网站上时,它们有一些对象跟随你的鼠标移动。我试图通过使粒子移动与玩家相同的量来实现这一点,但它没有复制预期的效果。有什么建议吗?我的代码:

声明粒子

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

移动方法

 -(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];

         }

    }
2个回答

15

试试这个:

1)如果你的发射器在场景中,使用该发射器的属性targetNode,并将其设置为Scene。这意味着粒子不会成为发射器的子节点,而是成为场景的子节点,应该会留下痕迹。

不确定这是否正确(我是用C#做的):

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

还有一些额外的内容:

2) 我认为你可以将发射器作为子级附加到 self.player 上,这样它就会随着玩家一起移动并自动调整位置,从而不需要进行以下操作:

self.trailParticle.position = [self checkBounds:newLocPart];
self.trailParticle.particleAction = [SKAction moveByX:translation.x y:-translation.y duration:0];

2
targetNode 是我需要的。谢谢。 - Charlie Martin

0
在你的更新循环中,检查你的玩家是否沿着X轴移动。 然后每次发生这种情况时创建一个精灵节点。在这个例子中,将你的玩家命名为"player1"。 关键在于你的粒子必须在出生率附近的粒子列中设置最大值。 以下代码对我有效。
-(void)update:(CFTimeInterval)currentTime {
// Find your player
SKNode* Mynode = (SKSpriteNode *)[self childNodeWithName:@"player1"];  

// Check if our player is moving. Lower the number if you are not getting a trail. 
 if (Mynode.physicsBody.velocity.dx>10|
 Mynode.physicsBody.velocity.dy>10|
 Mynode.physicsBody.velocity.dx<-10|
 Mynode.physicsBody.velocity.dy<-10){       


// Unpack your particle
NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"trail" ofType:@"sks"];

//Create your emitter
SKEmitterNode *myTrail = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];

// This ensures your trail doesn't stay forever . Adjust this number for different effects
myTrail.numParticlesToEmit=10; 

// The length of your trail - higher number, longer trail. 
myTrail.particleLifetime = 2.0; 

//Set the trail position to the player position   
myTrail.position=Mynode.position;

//Add the trail to the scene
[self addChild:myTrail];
}

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接