2

I'm working with Swift 3, SpriteKit and Xcode.

So I have a node named backgroundNode, and I attach every nodes of my game to this backgroundNode.

Now I would like to be able to zoom on my game with a pinch gesture, and when I'm zoomed in to navigate in my game.

I see 2 possibilities to do this :

  • deplace the background node and change its scale to zoom in and zoom out,
  • use SKCameraNode

What do you think is the best option ?

I already tried the first option but the zoom gesture is quite complex as if I scale the backgroundNode up when I want to zoom, the anchor point is in 0;0 and not 0.5;0.5 so it doesnt zoom where the pinch gesture is detected, but from the bottom right corner, I don't know if you see what I mean.

And for the second option, I can't manage to move the camera without having a glitchy effect, maybe my code is wrong but it really seems correct.

Can you help me ?

Edit : So I got it working using SKCameraNode and UIPanGestureRecognizer, here is the code :

var cam: SKCameraNode!

let panGesture = UIPanGestureRecognizer()

override func didMove(to view: SKView)
{       
    cam = SKCameraNode()
    camera = cam
    cam.position = CGPoint(x: playableRect.midWidth, y: playableRect.midHeight)
    addChild(cam)

    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(GameScene.panFunction))
    view.addGestureRecognizer(panGesture)
}

func panFunction(pan : UIPanGestureRecognizer)
{
    let deltaX = pan.translation(in: view).x
    let deltaY = pan.translation(in: view).y

    cam.position.x -= deltaX
    cam.position.y += deltaY

    pan.setTranslation(CGPoint(x: 0, y: 0), in: view)
}

Now I'm struggling with the Zoom. I tried using UIPinchGestureRecognizer but it doesn't work as good as the pan gesture, here is what I tried :

var firstPinch: CGFloat = 0

var pinchGesture = UIPinchGestureRecognizer()
let panGesture = UIPanGestureRecognizer()
var cam: SKCameraNode!

override func didMove(to view: SKView)
{
    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(GameScene.pinchFunction))
    view.addGestureRecognizer(pinchGesture)

    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(GameScene.panFunction))
    view.addGestureRecognizer(panGesture)
}

func pinchFunction(pinch : UIPinchGestureRecognizer)
{
    if UIGestureRecognizerState.began == pinch.state
    {
        firstPinch = pinch.scale
    }

    actualPinch = pinch.scale

    cam.xScale -= actualPinch - firstPinch
    cam.yScale -= actualPinch - firstPinch
}

How would you do it ?

Drakalex
  • 1,488
  • 3
  • 19
  • 39
  • Post the current code for option 2 (SKCameraNode). That's the way I would go about this. Alternatively, post the code for both attempts, and pick which we should help with – Nik Nov 20 '16 at 21:26
  • FYI: If you got option 1 to work, try to find a way to change the anchorPoint to 0.5,0.5. Usually that just involves a change in position – Nik Nov 20 '16 at 21:31
  • @Nik I just posted the code, I think the SKCameraNode option would do it better but I can't get it working – Drakalex Nov 21 '16 at 17:37

1 Answers1

0

You need to post your code. I helped someone with this in another forum. Their code + my answer should give a general idea of what to do:

https://forums.developer.apple.com/message/192823#192823

Basically it involves UIPanGestureRecognizer etc, and then a bit of delta-scale logic to adjust for the new bounds of the camera.

Fluidity
  • 3,985
  • 1
  • 13
  • 34
  • Okay, so I managed to get the camera moving properly with UIPanGestureRecognizer thanks to your post. Now how could I use UIPinchGestureRecognizer to scale the camera properly ? – Drakalex Nov 21 '16 at 17:52