Follow a path inverse

前端 未结 1 1015
你的背包
你的背包 2021-01-23 22:41

I\'m making a sprite to orbit a point and for that I made a path that is followed by that sprite:

    let dx1 = base1!.position.x - self.frame.width/2
    let dy         


        
相关标签:
1条回答
  • 2021-01-23 23:01

    A straightforward way to rotate a sprite about a point (i.e., orbit) is to create a container node, add a sprite to the node at an offset (relative to the container's center), and rotate the container node. Here's an example of how to do that:

    class GameScene: SKScene {
        let sprite = SKSpriteNode(imageNamed:"Spaceship")
        let node = SKNode()
    
        override func didMove(to view: SKView) {
            sprite.xScale = 0.125
            sprite.yScale = 0.125
            sprite.position = CGPoint (x:100, y:0)
    
            node.addChild(sprite)
            addChild(node)
    
            let action = SKAction.rotate(byAngle:CGFloat.pi, duration:5)
    
            node.run(SKAction.repeatForever(action), withKey:"orbit")
        }
    
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let action = node.action(forKey: "orbit") {
                // Reverse the rotation direction
                node.removeAction(forKey:"orbit")
                node.run(SKAction.repeatForever(action.reversed()),withKey:"orbit")
                // Flip the sprite vertically
                sprite.yScale = -sprite.yScale
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题