2013-12-21 37 views
7

我試圖讓它成爲我在玩家移動時跟隨玩家的粒子。我試圖複製的效果就像當你在網站上時,他們有一些隨鼠標移動的對象。我試圖通過讓粒子移動玩家的數量來做到這一點,但它並沒有重現預期的效果。有什麼建議麼?我的代碼:用SKEmitterNode和SpriteKit中的粒子創建一條軌跡

聲明顆粒

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方法

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

     } 

    } 

回答

11

試試這個:

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

targetNode是我所需要的。謝謝。 –

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

}