replacing CALayer and CABasicAnimation with SKScene and SKActions

后端 未结 2 1436
小蘑菇
小蘑菇 2020-12-08 16:27

I am trying to restructure this Github Swift project on Metaballs so that the circles are represented by SKShapeNodes that are moved around by SKActions instead of CAB

2条回答
  •  攒了一身酷
    2020-12-08 16:41

    Assuming that your question is how to replicate the same behaviour with nodes and SKAction, I believe this should do it.

    var shapeNode : SKNode?
    
    func startAnimation(){
    
        if let node = self.shapeNode {
            //assume that we have a node initialized at some point and added to the scene at some x1,y1 coordinates
    
            /// we define parameters for the animation
            let positionToReach = CGPoint(x: 100, y: 100) /// some random position
            let currentPosition = node.position /// we need the current position to be able to reverse the "animation"
            let animationDuration = 2.5 //loadingAnimation!.duration = 2.5
    
            /// we define which actions will be run for the node
            let actionForward = SKAction.moveTo(positionToReach, duration: animationDuration)
            let actionBackwards = SKAction.moveTo(currentPosition, duration: animationDuration)
    
            // we needed two actions to simulate loadingAnimation!.autoreverses = true
    
            /// we wrap the actions in a sequence of actions
            let actionSequence = SKAction.sequence([actionForward, actionBackwards]) /// animations to repeat
    
            /// we want to repeat the animation forever
            let actionToRepeat = SKAction.repeatActionForever(actionSequence) ///loadingAnimation!.repeatCount = Float.infinity
    
            /// showtime
            node.runAction(actionToRepeat)
    
        }
    
    }
    

    Let me know if I need to update any part as I haven't tested it. You still need to use your actual values and objects.

    I have referred to referred to How would I repeat an action forever in Swift? while making this reply.

提交回复
热议问题