如何让玩家在路径中移动到对面?

时间:2016-07-03 05:55:29

标签: swift path sprite-kit move

enter image description here

我希望当触摸开始时,播放器(红色圆圈)移动到圆形路径的另一侧。我已经让玩家遵循了一条道路,但我没有在互联网上找到我的问题的答案。

    override func didMoveToView(view: SKView) {

    player = SKSpriteNode(imageNamed: "circulo")
    player.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 170)
    player.color = colorGris
    player.colorBlendFactor = 1
    player.size = CGSize(width: 25, height: 25)
    self.addChild(player)
    player.zPosition = 3

   }
   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    if gameStarted == false {

        gameStarted = true
        moveClockWise()
        movingClockWise = true

    }

       }



   func moveClockWise(){



    let dx = player.position.x - self.frame.width / 2
    let dy = player.position.y - self.frame.height / 2

    let rad = atan2(dy, dx)

    path = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

    let follow = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, speed: 200)
    player.runAction(SKAction.repeatActionForever(follow).reversedAction())

}

2 个答案:

答案 0 :(得分:3)

在圆形路径中移动对象的最简单方法之一是

  1. 创建SKNode容器
  2. 创建一个精灵
  3. 将容器添加到场景中
  4. 将精灵的x位置设置为圆形路径的半径
  5. 将精灵添加到容器
  6. 旋转容器
  7. 如果您想将精灵移动到另一侧或更改旋转的半径,只需

    1. 更改精灵的x位置
    2. 如果要更改旋转方向,

      1. 扭转容器的旋转
      2. 示例代码:

        // 1) Create the container node 
        let node = SKNode()
        // 2) Create a sprite
        let sprite = SKSpriteNode(color:SKColor.blueColor(),size:CGSizeMake(20,20))
        var rotation:CGFloat = CGFloat(M_PI)
        let radius:CGFloat = 50
        
        override func didMoveToView(view: SKView) {
            scaleMode = .ResizeFill
            node.position = view.center
            // 3) Add the container to the scene
            addChild(node)
            // 4) Set the sprite's x position
            sprite.position = CGPointMake(radius, 0)
            // 5) Add the sprite to the container
            node.addChild(sprite)
            // 6) Rotate the container
            rotate()
        }
        
        // Rotate the container
        func rotate() {
            let action = SKAction.rotateByAngle(rotation, duration: 4)
            node.runAction(SKAction.repeatActionForever(action),withKey: "rotate")
        }
        
        // 8) Reverse the direction of the rotation
        func reverse() {
            rotation = -rotation
        }
        
        // Stop rotating the container
        func stopRotation() {
            if node.actionForKey("rotate") != nil {
                node.removeActionForKey("rotate")
            }
        }
        
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
           /* 7) Change the sprite's x-position  */
            if sprite.actionForKey("move") == nil {
                stopRotation()
                let opposite = -sprite.position.x * 2
                let move = SKAction.moveByX(opposite, y: 0, duration: 3)
                let rotate = SKAction.runBlock {
                    self.rotate()
                }
                sprite.runAction(SKAction.sequence([move, rotate]), withKey: "move")
            }
        }
        

答案 1 :(得分:1)

我认为您可以按照以下步骤操作:

  • 停止播放器的runAction

你可以这样做:

let follow = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, speed: 200)
player.runAction(SKAction.repeatActionForever(follow).reversedAction(),withKey:"followPath")

停止做简单:

player.removeActionForKey("followPath")
  • 重新建立自己的道路,以便有一个新的&#34; moveToPoint&#34; (起点)具有实际位置:

为此,我尝试使用您的代码使其易于理解:

var myCircle : CGMutablePath! = CGPathCreateMutable()
let newDx = player.position.x - self.frame.width / 2
let newDy = player.position.y - self.frame.height / 2
let newRad = atan2(newDy, newDx)
let newPath = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: newRad, endAngle: newRad + CGFloat(M_PI * 4), clockwise: true)
  • 镜像路径:

要做到这一点,你可以写:

var mirroring = CGAffineTransformMakeScale(1.0, -1.0) // flip horizontal
var mirrorPath : CGMutablePath! = CGPathCreateMutable()
CGPathAddPath(mirrorPath, &mirroring, newPath.CGPath)
  • 重新启动runAction

您可以在这里重新启动:

let newFollow = SKAction.followPath(mirrorPath, asOffset: false, orientToPath: true, speed: 200)
player.runAction(SKAction.repeatActionForever(newFollow).reversedAction(),withKey:"followPath")

<强>加成: 如果你可以添加一些漂亮的动画,例如点位置和圆圈中的镜像点之间的跳转,你需要知道CGPoint目的地(在mirrorPath中它将是&#34; moveToPoint&#34 ;或者也是第一点)。 Here您可以找到获取所有CGPath点的扩展程序,以便:

var mirrorPoints = mirrorPath.getPathElementsPoints()
let destinationPoint = mirrorPoints.first!

在Sprite-kit框架中,可用的SKAction's之间还没有jumpAction,所以你可以用很少的代码创建它。
通常是&#34;跳跃&#34;通过更改Y坐标来制作,在您的情况下,您的视图是从高处开始,因此您可以进行缩放(缩放)。