2017-08-29 64 views
0

我想让我的宇宙飞船拥有跟随它的粒子轨迹。我将如何做到这一点?Spritekit添加粒子

这是我为我的飞船代码:

override func didMove(to view: SKView) { 
    spaceS.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 
    spaceS.setScale(0.05) 
    spaceS.zPosition = 1 

    addChild(spaceS) 

} 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

     for touch in touches { 
      let location = touch.location(in: self) 
      spaceS.position = CGPoint(x: location.x, y: location.y) 
     } 


} 
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
       for touch in touches { 
      let location = touch.location(in: self) 
      spaceS.position = CGPoint(x: location.x, y: location.y) 
     } 


} 

编辑1:火灾粒子踪迹更确切地说

回答

1

首先,你需要通过按下命令+ N创建SpriteKit Particle File键盘和从模板下的iOSResources→选择SpriteKit Particle File点击Next并选择particle templatefire按nex t并命名为SpriteKit Particle File这将创建一个yourFileName.sks文件到您的项目。

使用下面的代码:

override func didMove(to view: SKView) { 

    spaceS = SKSpriteNode(imageNamed: "Spaceship") 
    spaceS.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 
    spaceS.setScale(0.50) 
    spaceS.zPosition = 1 

    addChild(spaceS) 

    let emitter = SKEmitterNode(fileNamed: "MyParticle") //MyParticle is the name of the sks file. 
    emitter?.position = CGPoint(x: 0, y: -spaceS.size.height) 
    emitter?.zPosition = 1 
    spaceS.addChild(emitter!) //Now the emitter is the child of your Spaceship.  
} 

输出:

enter image description here

+0

如果精灵太小火没有出现,而且它也出现在了中间sprite –

+0

我会更好地解释我的最后一条评论。如果粒子设置为较小的比例,它会出现在精灵的中间(它不会消失) –

+0

也许你的精灵比较小。试着缩小你的粒子的大小。如果我的答案有助于解决问题,您应该接受答案或upvote。 – Joe