遵循反向路径 [英] Follow a path inverse

查看:136
本文介绍了遵循反向路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个精灵来运行一个点,为此我创建了一个跟随该精灵的路径:

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

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

That works and the sprite starts to orbit around that point. The thing is that when the user touches the screen I want that sprite to start rotating counterclockwise. For that I write the same code for rotating clockwise but editing the last line to:

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

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

But the problem is that although it rotates counterclockwise, the sprite image flips horizontal. What can I do to avoid that? Or is there any other way to orbit a sprite through a point?

推荐答案

一个直接的方法来旋转精灵点(即,轨道)是创建一个容器节点,在偏移处(相对于容器的中心)向节点添加一个精灵,并旋转容器节点。以下是如何执行此操作的示例:

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

这篇关于遵循反向路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆