2013-12-21 8 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]; 
} 

} 
関連する問題