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

         }

    }
4

2 回答 2

15

尝试这个:

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

不确定这是否正确(我在 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];
于 2014-01-14T14:43:08.283 回答
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];
}

}
于 2014-04-03T02:51:23.133 回答