How to make SKAction Oscillation move faster and faster?

43 views Asked by At

Looking to make an SKSprite (road in this case) move back and forth across the screen. I'd like for the road to move faster and faster every second. I implemented the following but it only sets the oscillation once and never gets updated. Is there any way I can make it oscillate faster and faster? Or is there a better way of doing this using something else?

var wallSpeedMultiplier = 1.0

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !gameStarted {
            titleView.removeFromParent()
            tapToStart.removeFromParent()
            gameStarted = true
            let wait = SKAction.wait(forDuration: 1.0)
            let block = SKAction.run({
                [unowned self] in
                wallSpeedMultiplier = wallSpeedMultiplier - 0.01
            })
            let sequence = SKAction.sequence([wait, block])
            run(SKAction.repeatForever(sequence), withKey: "timer")
            let oscillate = SKAction.oscillation(amplitude: 200, timePeriod: (30*wallSpeedMultiplier), midPoint: road!.position)
            road!.run(SKAction.repeatForever(oscillate))
            road!.run(SKAction.moveBy(x: size.width, y: 0, duration: 5))
        }
    }

extension SKAction {
    static func oscillation(amplitude a: CGFloat, timePeriod t: CGFloat, midPoint: CGPoint) -> SKAction {
        let action = SKAction.customAction(withDuration: Double(t)) { node, currentTime in
            let displacement = a * sin(2 * π * currentTime / t)
            node.position.x = midPoint.x + displacement
        }
        return action
    }
}
0

There are 0 answers