如何将粒子附加到移动的物体上

How to attach particles to moving object

我创建了一个 PowerUpTrail.sks(粒子东西) 我正在尝试将它附加到移动的物体上

func SpawnPowerUp(){

    let PowerUp = SKSpriteNode(imageNamed: "PowerUp.png")
    let MinValue = self.size.width / 8
    let MaxValue = self.size.width - 20
    let SpawnPoint = UInt32(MaxValue - MinValue)
    PowerUp.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height - 100)

    //Physics
    PowerUp.physicsBody = SKPhysicsBody(rectangleOfSize: PowerUp.size)
    PowerUp.physicsBody?.categoryBitMask = PhysicsCategory.PowerUp //Sätter unikt ID för Enemy, hämtat från PhysicsCategory som vi gjorde där uppe
    PowerUp.physicsBody?.contactTestBitMask = PhysicsCategory.PowerUp //Kolla om Enemy nuddar Bullet
    PowerUp.physicsBody?.affectedByGravity = false
    PowerUp.physicsBody?.dynamic = true

    let action = SKAction.moveToY(-128, duration: 5)
    let actionDone = SKAction.removeFromParent()
    PowerUp.runAction(SKAction.sequence([action, actionDone]))
    PowerUp.runAction(SKAction.rotateByAngle(5, duration: 5))


    /* this does not work!
    let PowerUpTrail = SKEmitterNode(fileNamed: "PowerUpTrail.sks")
    PowerUpTrail!.targetNode = self
    PowerUpTrail!.position = PowerUp.position
    self.addChild(PowerUpTrail!)
    */

    self.addChild(PowerUp)

}

粒子生成但不跟随对象。我该如何解决?

使一个节点的位置跟踪另一个节点的最简单方法是使用节点层次结构:使粒子发射器成为通电精灵的子节点。

let powerUp = SKSpriteNode(imageNamed: "PowerUp.png")
// ...
let powerUpTrail = SKEmitterNode(fileNamed: "PowerUpTrail.sks")!
powerUpTrail.targetNode = self
powerUp.addChild(powerUpTrail)

(另请注意一些 Swift 样式提示:首字母大写通常仅用于类型名称;对变量名称使用首字母小写字母。并在创建时从 SKEmitterNode(fileNamed:) 中解包一次可选而不是以后每次使用它。)