swift - 如何让玩家在路径中 move 到另一边?

标签 swift path sprite-kit move

enter image description here

我希望当触摸开始时玩家(红色圆圈) move 到圆形路径的另一侧。我已经让玩家跟随一条路径,但我还没有在互联网上找到我的问题的答案。

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

}

最佳答案

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

  1. 创建一个SKNode容器
  2. 创建 Sprite
  3. 将容器添加到场景
  4. 将 Sprite 的 x 位置设置为圆形路径的半径
  5. 将 Sprite 添加到容器中
  6. 旋转容器

如果你想将 Sprite move 到另一边或改变旋转的半径,只需

  1. 改变 Sprite 的x位置

如果要改变旋转方向,

  1. 反转容器的旋转

示例代码:

// 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")
    }
}

关于swift - 如何让玩家在路径中 move 到另一边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38167139/

相关文章:

ios - ScrollView 对齐问题

java - 错误 : Could not find or load main class

iOS:根据数组的顺序对文件夹中的文件进行排序

sprite-kit - SKShader 创建视差背景

ios - 如何为 UITableViewCell 的 .selectedBackgroundView 设置自定义边距

swift - 合并两个元组数组

swift - 如何快速计算 Sprite 的数量

ios - 没有 init 方法时如何正确子类化 SKNode?

ios - Swift 核心图像在没有 CIFilter 的情况下对图像应用锐化

python - PYTHONPATH 和 PYTHON_LIBRARY_PATH 是什么?