swift - Node的路径粒子效果与SpriteKit

标签 swift path sprite-kit particles skemitternode

我正在使用 Swift、Sprite Kit 和 Xcode 6,

我想在 SpriteKit 中创建一个粒子效果,有点像 iOS 游戏“Duet”中球的粒子效果,但我不知道如何进行,我设法创建了一个粒子效果,但不是像这个游戏中那样跟随节点并绘制节点路径的粒子...

这是我的代码:

let firstCircle = SKSpriteNode(imageNamed: "Circle")
let particle = SKEmitterNode(fileNamed: "FirstParticle.sks")

override func didMoveToView(view: SKView)
{
    firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
    firstCircle.physicsBody?.affectedByGravity = false

    particle.targetNode = firstCircle

    addChild(firstCircle)
    addChild(particle)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{        
    for touch: AnyObject in touches
    {
        firstCircle.position = touch.locationInNode(self)
    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
{
    for touch: AnyObject in touches
    {
        firstCircle.position = touch.locationInNode(self)
    }
}

最佳答案

要实现类似 Duet 的效果,您需要将 particle 作为尾随节点的子节点,并将 targetNode 设置为父场景。 targetNode 控制将粒子渲染为哪个节点的子节点。

particle 是尾随节点的子节点时,它将以尾随节点为原点发射粒子。将 targetNode 更改为父场景会在尾随节点移动时留下已发射的粒子。

此代码应该可以工作,但您可能需要微调 FirstParticle.sks

let firstCircle = SKSpriteNode(imageNamed: "Circle")
let particle = SKEmitterNode(fileNamed: "FirstParticle.sks")

override func didMoveToView(view: SKView)
{
    firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
    firstCircle.physicsBody?.affectedByGravity = false

    particle.targetNode = self

    addChild(firstCircle)
    firstCircle.addChild(particle)
}

我能够获得类似的效果,并最终创建了一个 Playground 来演示它。看看here .

Demo of Duet trail effect

关于swift - Node的路径粒子效果与SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459417/

相关文章:

swift - 玩家在移动时口吃 - Sprite Kit

ios - 在 12 小时制设备中使用 Date()

ios - 如何在此 iOS 条形图上将这些 double 转换为整数?

php - Symfony2 : Get Route Path

ios - 循环没有使动画工作,swift 2

ios - 来自关卡编辑器的 SpriteKit 引用节点

ios - 使用 swift 3 将字符串从开始位置剪切到结束位置

iOS swift : Crash on deleteRowsAtIndexPaths

java - 在 tomcat webapp 中文件的位置

powershell - 在 Powershell 中对路径列表进行排序