1

In a game I'm building a user is able to move the camera around by dragging around the screen similar to a scrollview. This is all handled in the main scene file by the touchesBegan and touchesMoved method. However I also want the ability for a user to drag from an SKSpriteNode (not actually moving it) to another SKSpriteNode - I then perform an action based on the 2 nodes. I can do both of these fine separately. But when a user is dragging from one node to another, I don't want the camera to pan.

Again I've managed to resolve this using a boolean flag that's set on touchesBegan if they have touched a node. However the screen then doesn't pan if a user starts touching on one of the sprites but finishes off of one e.g they actually quickly performed a pan that happened to start on a node. Does anyone have any good solutions for this?

One thought I had was to store all the touch events in touchesMoved, and then loop through them in touchesEnded performing the same movement logic, provided they didn't start and end on a sprite. e.g

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches {
            movingTouches.append(touch)
        }
    }


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {


        if startedOnSprite && !endedOnSprite || !(startedOnSprite && endedOnSprite) {

                let location = movingTouches.last().location(in: self)
                let previousLocation = movingTouches.first().location(in: self)
                let deltaY = location.y - previousLocation.y
                let deltax = location.x - previousLocation.x


                gameCamera?.position.y -= deltaY

                view.frame.size.height
                gameCamera?.position.y = (gameCamera?.position.y)! > viewHeight/2 ? viewHeight/2 : (gameCamera?.position.y)!
                gameCamera?.position.y = (gameCamera?.position.y)! < -(viewHeight/2) ? -(viewHeight/2) : (gameCamera?.position.y)!
                gameCamera?.position.x -= deltax
                gameCamera?.position.x = (gameCamera?.position.x)! < -(viewWidth/2) ? -(viewWidth/2) : (gameCamera?.position.x)!
                gameCamera?.position.x = (gameCamera?.position.x)! > (viewWidth/2) ? (viewWidth/2) : (gameCamera?.position.x)!
            }

        movingTouches = [UITouch]()
        endedOnSprite = false
        startedOnSprite = false
    }

To complicate matters I also want the Sprites to have a tap event on them as well as dragging. I'm struggling to find a good way to do all this in SpriteKit

Unfortunately the above isn't very smooth at all, and in think it appears to not even scroll the distance I would expect either (the exact same code does scroll correctly if I'm not bothered about whether or not it starts on a Sprite)

So to be clear, I want scrolling behaviour, provided a user isn't actually using a finger to 'draw' from one sprite to another

TommyBs
  • 9,354
  • 4
  • 34
  • 65

1 Answers1

0

Gesture recognisors on your scene would do what you need, keeping taps and swipes separated.

Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23
  • Would they? In theory dragging from one sprite to another would get classed as a swipe would it not resulting in the panning behaviour? – TommyBs Jan 10 '17 at 21:59
  • You have a lot of fine control in the gesture recognisors with state indicating if the gesture has completed. – Mark Brownsword Jan 10 '17 at 22:11
  • Also, each gesture is distinct and designed not to interfere with each other and different kinds of panning could be handled by knowing what is the target location i.e. a sprite or background node? – Mark Brownsword Jan 11 '17 at 07:00
  • I appreciate there's the control over the gesture recognizers, but I'm still not sure if they will allow me to do the control I want of being able to drag a node but if it doesn't finish on another node perform the scroll instead. Saying that I will investigate this post here http://stackoverflow.com/questions/5111828/how-to-have-a-uiswipegesturerecognizer-and-uipangesturerecognizer-work-on-the-sa when I have a bit more time and see if I can accomplish what I want to do. Thanks for your input – TommyBs Jan 11 '17 at 07:06