【问题标题】:Follow a path inverse遵循反向路径
【发布时间】:2016-03-18 13:43:44
【问题描述】:

我正在制作一个围绕一个点运行的精灵,为此我制作了一条该精灵遵循的路径:

    let dx1 = base1!.position.x - self.frame.width/2
    let dy1 = base1!.position.y - self.frame.height/2

    let rad1 = atan2(dy1, dx1)

    path1 = UIBezierPath(arcCenter: circle!.position, radius: (circle?.position.y)! - 191.39840698242188, startAngle: rad1, endAngle: rad1 + CGFloat(M_PI * 4), clockwise: true)
    let follow1 = SKAction.followPath(path1.CGPath, asOffset: false, orientToPath: true, speed: 200)
    base1?.runAction(SKAction.repeatActionForever(follow1))

这行得通,精灵开始围绕该点运行。问题是当用户触摸屏幕时,我希望该精灵开始逆时针旋转。为此,我编写了相同的顺时针旋转代码,但将最后一行编辑为:

base1?.runAction(SKAction.repeatActionForever(follow1).reversedAction())

但问题是,虽然它是逆时针旋转的,但精灵图像是水平翻转的。我能做些什么来避免这种情况?或者有没有其他方法可以让精灵绕着一个点运行?

【问题讨论】:

    标签: ios rotation sprite-kit


    【解决方案1】:

    围绕一个点(即轨道)旋转精灵的一种直接方法是创建一个容器节点,将一个精灵添加到该节点的偏移处(相对于容器的中心),然后旋转容器节点。以下是如何做到这一点的示例:

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

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2020-09-24
      • 1970-01-01
      • 2013-02-26
      相关资源
      最近更新 更多