在SpriteKit Swift中移动相机

时间:2016-08-10 06:28:38

标签: swift camera sprite-kit skscene pan

我正在使用swift在sprite工具包中创建一个游戏,我试图能够用手指移动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)
    }

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

4 个答案:

答案 0 :(得分:4)

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

同时在创建手势识别器时,您可以按maximumNumberOfTouches:minimumNumberOfTouches:配置触摸次数。

答案 1 :(得分:4)

作为@ 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
  }
}

我没有跑这个,只是在操场上写下来。另请注意,如果您还要处理其他点击/手势,则必须编写其他代码,以确保识别适用于您所有预期的场景。

答案 2 :(得分:4)

如果有人需要,这是一个多功能的解决方案:

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
  }

}

答案 3 :(得分:0)

如果您发现水平移动与您期望的水平移动相反,请尝试以下(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
}