How to limit the movement of two anchored lines so they swing continually like a pendulum

后端 未结 2 1154
囚心锁ツ
囚心锁ツ 2020-12-16 12:36

I have created two lines anchored to a sprite, which are 30˚ apart. I want both lines to swing left and right like a pendulum, always swinging from end to end (in such a way

2条回答
  •  离开以前
    2020-12-16 13:17

    A swift 5 extension version for the good mogelbuster code :

    extension SKAction {
        class func pendulum(withAngle angle:CGFloat, period:TimeInterval, key:String) -> SKAction {
            let initialRotate = SKAction.rotate(byAngle: angle/2, duration: period/2)
            initialRotate.timingMode = .easeOut
            let rotate = SKAction.rotate(byAngle: angle, duration: period)
            rotate.timingMode = .easeInEaseOut
            let rotateForever = SKAction.repeatForever(SKAction.sequence([rotate.reversed(), rotate]))
            return SKAction.sequence([initialRotate, rotateForever])
        }
    }
    

    Usage:

    let pendulum = SKAction.pendulum(withAngle: CGFloat.pi/2, period: 0.5, key: "pendulum")
    self.mynode.run(pendulum)
    

提交回复
热议问题