我创建了一个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)
}
粒子产生但它不跟随对象。我该如何解决这个问题?
答案 0 :(得分:1)
使一个节点的位置跟踪另一个节点的最简单方法是使用节点层次结构:使粒子发射器成为加电精灵的子节点。
let powerUp = SKSpriteNode(imageNamed: "PowerUp.png")
// ...
let powerUpTrail = SKEmitterNode(fileNamed: "PowerUpTrail.sks")!
powerUpTrail.targetNode = self
powerUp.addChild(powerUpTrail)
(另请注意一些Swift样式提示:initial-cap通常仅用于类型名称;对变量名称使用initial-lowercase。在创建可选项时从SKEmitterNode(fileNamed:)
解包您的可选项而不是每次使用时之后。)