我想让我的宇宙飞船有一个跟随它的粒子踪迹。我该怎么做?
这是我的宇宙飞船的代码:
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:火焰粒子追踪更准确
答案 0 :(得分:2)
首先,您需要通过键盘上的 命令+ n 以及SpriteKit Particle File
下的模板 - &gt;创建iOS
。 Resources
- &gt;选择SpriteKit Particle File
点击Next
并选择particle template
至fire
,然后按下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.
}
<强>输出:强>