1

I have two SKSpriteKitNodes, one in the left and one in the right. Then I detect where is the location of the touch and make the action according to its location (the left side shoots, and the right side speeds up). But the thing is, if the left is being touched, how can I handle the right-side touch at the same time?

The code I have so far:

let left = SKSpriteNode()
let right = SKSpriteNode()

override func didMoveToView(view: SKView) {

    left.size = CGSize(width: frame.width / 2, height: frame.height)
    right.size = CGSize(width: frame.width / 2, height: frame.height)

    left.position = CGPoint(x: frame.width / 4, y: frame.height / 2)
    right.position = CGPoint(x: 3 * frame.width / 4, y: frame.height / 2)

}



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches{
        let location = touch.locationInNode(self)

        if right.containsPoint(location){
            //HANDLE THE SPEED

            if speed == 0{
                speed = 5
            }
            shouldBreak = false
        }
        if left.containsPoint(location){
            //HANDLE THE SHOOTING
            shoot.position = CGPoint(x: frame.midX, y: frame.height * 0.2)
            isShooting = true
        }


    }
  • You can use one flag per node to know if each node is touched. You can set to true in the touchesbegan and false in the touchesended and also when the left node is touched you check the right flag and viceversa to know if you have to do any action you need to do. – CrApHeR Jul 06 '15 at 18:03
  • https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: – Aggressor Jul 06 '15 at 18:07
  • Look at 'Controlling Simultaneous Gesture Recognition' – Aggressor Jul 06 '15 at 18:08
  • @CrApHeR I didn't really understand what you said, can you explain how to do it? – Gabriel Bitencourt Jul 06 '15 at 18:33
  • @Aggressor do you think that using UIGestureRecognizer is really necessary when using SpriteKit? I mean, I don't know, but there must be a way to do it – Gabriel Bitencourt Jul 06 '15 at 18:35
  • Don't be afraid to get your hands dirty, that's how you learn! – Aggressor Jul 06 '15 at 18:47
  • http://stackoverflow.com/questions/27343926/multi-touch-gesture-in-sprite-kit – 0x141E Jul 06 '15 at 18:53
  • @GabrielBitencourt It is too difficult to explain in the comments section because the space to write is not enough, but you can see the 0x141E comment, it can help you with the problem. – CrApHeR Jul 06 '15 at 18:58

0 Answers0