Spritekit添加粒子

时间:2017-08-29 17:49:30

标签: ios swift3 sprite-kit particles

我想让我的宇宙飞船有一个跟随它的粒子踪迹。我该怎么做?

这是我的宇宙飞船的代码:

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 个答案:

答案 0 :(得分:2)

首先,您需要通过键盘上的 命令+ n 以及SpriteKit Particle File下的模板 - &gt;创建iOSResources - &gt;选择SpriteKit Particle File点击Next并选择particle templatefire,然后按下SpriteKit Particle File,这将创建一个 yourFileName.sks < / strong>提交给您的项目。

使用以下代码:

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