在 SpriteKit 中移动相机 Swift

Moving Camera in SpriteKit Swift

我正在使用 swift 在 sprite kit 中创建一个游戏,我试图用手指移动 SKScene,因为并非所有节点都适合场景。我已经用这段代码创建了世界、叠加层和相机节点。

        override func didMoveToView(view: SKView) {
    world = self.childNodeWithName("world")!

    if !isCreated {
        isCreated = true

        // Camera setup
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        self.world = SKNode()
        self.world.name = "world"
        addChild(self.world)

        self.cam = SKNode()
        self.cam.name = "camera"
        self.world.addChild(self.cam)

        // UI setup
        self.overlay = SKNode()
        self.overlay.zPosition = 10
        self.overlay.name = "overlay"
        addChild(self.overlay)
    }

我希望能够通过单指平移手势来移动相机。我该怎么做?任何帮助将不胜感激。

首先你必须创建 UIPanGestureRecognizer。然后将它添加到场景的视图中。在手势识别器的 action 方法中,您可以使用 translationInView:。在此基础上,您可以修改相机的position

此外,在创建手势识别器时,您可以通过 maximumNumberOfTouches: and minimumNumberOfTouches: 配置触摸次数。

作为@Kris 解决方案(基于 UIKit)的替代方案,您还可以使用 SKScene 子类在 Sprite Kit 中监视触摸。我写了一个小示例,应该可以为您指明正确的方向。

class YourSceneSubclass : SKScene
{  
  override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let touch = touches.first else {
      return
    }

    let location = touch.locationInNode(self)
    let previousLocation = touch.previousLocationInNode(self)

    camera?.position.x += location.x - previousLocation.x
    camera?.position.y += location.y - previousLocation.y
  }
}

我没有运行这个,只是在操场上写的。另请注意,如果您还想处理其他 taps/gestures,则必须编写额外的代码以确保识别适用于所有预期场景。

如果您发现您的水平移动与您期望的相反,请尝试以下操作 (Swift 3):

let panGesture = UIPanGestureRecognizer()    
var previousPoint = CGPoint.zero

func  addPanGesture()
    {
        self.panGesture.addTarget(self, action:#selector(BaseScene.pannedView(_:) ))
        self.panGesture.delegate = self

        self.view?.addGestureRecognizer(self.panGesture)
    }
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool
{
    self.previousPoint = self.camera.position

    return true
}


func pannedView(_ sender:UIPanGestureRecognizer)
{
    let transPoint = sender.translation(in: self.view)

    let newPosition = self.previousPoint + (CGPoint(x: transPoint.x * -1.0, y: transPoint.y * 1.0))

    self.camera.position = newPosition
}

如果有人需要,这里有一个一体化的解决方案:

class GameScene: SKScene {

  var previousCameraPoint = CGPoint.zero

  override func didMove(to view: SKView) {
    let panGesture = UIPanGestureRecognizer()
    panGesture.addTarget(self, action: #selector(panGestureAction(_:)))
    view?.addGestureRecognizer(panGesture)
  }

  @objc func panGestureAction(_ sender: UIPanGestureRecognizer) {
    // The camera has a weak reference, so test it
    guard let camera = self.camera else {
      return
    }
    // If the movement just began, save the first camera position
    if sender.state == .began {
      previousCameraPoint = camera.position
    }
    // Perform the translation
    let translation = sender.translation(in: self.view)
    let newPosition = CGPoint(
      x: previousCameraPoint.x + translation.x * -1,
      y: previousCameraPoint.y + translation.y
    )
    camera.position = newPosition
  }

}