How to create travelling wave in SpriteKit
Asked Answered
C

1

1

How can I create a visual effect of traveling wave like this in Swift SpriteKit?

teKIT

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))
    }
}
Chide answered 13/5, 2020 at 22:43 Comment(0)
C
0

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.

Corse answered 17/5, 2020 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.