如何在SpriteKit中创建行波 [英] How to create travelling wave in SpriteKit
问题描述
如何在Swift SpriteKit中创建像这样的行波的视觉效果?
How can I create a visual effect of traveling wave like this in Swift SpriteKit?
我正在使用 SKAction
的扩展,该扩展在节点中执行振荡运动,但我仍然不知道如何创建跟踪。我虽然在创建副本,但没有用。
I am using an extension to the SKAction
that performs the oscillatory movement in the node, but I still do not know how to create the trail. I though in creating copies but it did not worked.
extension SKAction {
static func oscillation(scene: SKScene, amplitude a: CGFloat, timePeriod t: CGFloat, midPoint: CGPoint) -> SKAction {
let action = SKAction.customAction(withDuration: Double(t)) { node, currentTime in
let displacement = a * sin(2 * .pi * currentTime / t)
node.position.y = midPoint.y + displacement
let copy = node.copy() as! SKSpriteNode
copy.position.x = node.position.x
copy.position.y = node.position.y
scene.addChild(copy)
}
return action
}
}
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let node = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 50, height: 50))
node.position = CGPoint(x: 25, y: size.height / 2)
self.addChild(node)
let oscillate = SKAction.oscillation(amplitude: 200, timePeriod: 1, midPoint: node.position)
node.runAction(SKAction.repeatActionForever(oscillate))
node.runAction(SKAction.moveByX(size.width, y: 0, duration: 5))
}
}
推荐答案
我会尝试将粒子发射器附加到移动的节点上。可以将其设置为足够快地产生点状粒子,使其重叠并形成一条线。粒子将以恒定的速度向左移动,没有变化或褪色。将计数设置得足够高,以使最老的粒子消失之前看不见。如果您不需要实线,则可以降低发射器速率,并用虚线表示行波。粒子发射器中有很多选项,因此您可以尝试一些设置,以获得适合项目的其他效果。
I would try a particle emitter attached to the moving node. It could be set to produce dot shaped particles quickly enough that they would overlap and make a line. The particles would move at a constant speed to the left, with no variations or fade. Set the count high enough that the oldest particles go out of view before disappearing. If you don’t need a solid line, you could cut down the emitter rate and make a dotted line showing the traveling wave. There are oodles of options in particle emitters, so you could play around with the settings to get other effects if appropriate for your project.
这篇关于如何在SpriteKit中创建行波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!