How to create travelling wave in SpriteKit

丶灬走出姿态 提交于 2020-05-16 04:09:19

问题


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

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))
    }
}

来源:https://stackoverflow.com/questions/61786165/how-to-create-travelling-wave-in-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!