0

I currently have multiple balls all on the same SKScene. I handle all touches and gestures within GameScene. Below is the code I use to detect which node was touched, which works.

What I am unsure of, since there are always some touchesMoved when using this on a real device, is there anyway possible for more than one node to receive a tap at the same time? If so I obviously would need to write my code differently.

@objc func tappedView(_ sender:UITapGestureRecognizer) {

    if sender.state == .ended{
        let point : CGPoint = sender.location(in: self.view)
        var post = sender.location(in: sender.view)
        post = self.convertPoint(fromView: post)
        
        if let touchNode = self.atPoint(post) as? MyBall{
            //the declaration below is just so I have somewhere to stop in the debugger
            var x = 1 
        }
     }
}
LuisC329
  • 131
  • 8
  • https://stackoverflow.com/questions/27343926/multi-touch-gesture-in-sprite-kit – sangony Dec 02 '20 at 22:05
  • In general, that's not how you develop a game with Sprite Kit. Why do you want to use a gesture when you are not using UIKit? – El Tomato Dec 02 '20 at 22:58
  • @ElTomato I'll research that, though not using storyboards. Right now just want to make sure how to flip through multiple SKNodes, in case a tap does somehow occur at a CGPoint where there is more than one SKNode. – LuisC329 Dec 03 '20 at 02:26
  • @ElTomato, SpriteKit works off of UIKit, using a gesture is a perfectly acceptable way to handle interaction. – Knight0fDragon Dec 04 '20 at 06:38
  • @Knight0fDragon It certainly does. There is little point of using it. – El Tomato Dec 04 '20 at 06:49
  • @ElTomato there are lots of reasons to use it... You get the consistent response time you would from all other apps. One common gesture you would find in sprite games is the pinch gesture, to allow for zooming if a game is designed to zoom. – Knight0fDragon Dec 04 '20 at 06:52
  • @Knight0fDragon Okay, okay... That's good to know. I won't argue since I haven't developed a Sprite Kit game for three years or so. – El Tomato Dec 04 '20 at 06:53

1 Answers1

0

Use nodes(at:) to get multiple nodes at a point.

@objc func tappedView(_ sender:UITapGestureRecognizer) {

    if sender.state == .ended{
        let point : CGPoint = sender.location(in: self.view)
        var post = sender.location(in: sender.view)
        post = self.convertPoint(fromView: post)
        

        for touchNode in self.nodes(at:post){
            //the declaration below is just so I have somewhere to stop in the debugger
            var x = 1 
        }
     }
}

https://developer.apple.com/documentation/spritekit/sknode/1483072-nodes

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • Sorry, if you look at the documentation you would have seen the fix, I am used to old school swift – Knight0fDragon Dec 04 '20 at 21:07
  • Thank you, the actual syntax turned out to be: "for touchNode in self.nodes(at: post)" – LuisC329 Dec 04 '20 at 21:13
  • Yes, I already updated the answer to reflect it – Knight0fDragon Dec 04 '20 at 21:14
  • yes, I know. I thanked you. I swore I put up your name in the thanks. I actually come back just now to explain what music look like absolute laziness. I'm not dyslex, but there is something that makes me a bad reader. Not just laziness. – LuisC329 Dec 05 '20 at 01:28