ios - 遵循相反的路径

标签 ios rotation sprite-kit

我正在制作一个围绕一个点运行的 Sprite ,为此我制作了一条由该 Sprite 跟随的路径:

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

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

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

但问题是虽然逆时针旋转,但是sprite图像水平翻转了。我该怎么做才能避免这种情况?或者有没有其他方法可以让 Sprite 通过一个点绕轨道运行?

最佳答案

围绕一个点(即轨道)旋转 Sprite 的一种直接方法是创建一个容器节点,将一个 Sprite 添加到节点的偏移量(相对于容器的中心),然后旋转容器节点。以下是如何执行此操作的示例:

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

关于ios - 遵循相反的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34243964/

相关文章:

CSS 变换,chrome 中的锯齿状边缘

ios - iOS 7.1 中的 SpriteKit bodyWithTexture 等价物

ios - Sprite Kit 游戏场景在不应有的情况下发生变化

iphone - iOS - 文件是为存档构建的,不是链接的体系结构 (i386)

algorithm - 计算井字游戏唯一状态的有效算法

c# - Xamarin.forms - Masterdetailpage : System. Reflection.TargetInvocationException:调用目标引发了异常

c++ - 在 C++ 中将像素阵列旋转 90 度

swift - 如何分层 UIButtons 和 SpriteNodes

iphone - 在哪里保存 100 个 NSString + 其他类型

ios - 移动 Safari 中的文件上传和 EXIF