ios - Is there a way in Sprite Kit to get coordinates of a finger that doesn't move at the moment? -


to avoid unexpected game behaviour i'd know coordinates of fingers on touchscreen @ given moment. there way of doing this?

for example coordinates of fist finger when second taken off screen.

thanks in advance!

here's solution based on trojanfoe's idea.

import spritekit  class gamescene : skscene {  struct fingertrackdata {     var fingerid : int // used identify fingers in other part of code     var lastlocation : cgpoint // location of each finger }  // array stores information each finger coordinates var fingertracks : [fingertrackdata] = []   override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) {      touch in touches {          let location = touch.locationinnode(self)          // getavailablefingerid() returns available fingerid avoid id duplication         self.fingertracks.append(fingertrackdata(fingerid: self.getavailablefingerid(), lastlocation: location))      } }   override func touchesmoved(touches: set<uitouch>, withevent event: uievent?) {      touch in touches {          let location = touch.locationinnode(self)          self.updatefingertracks(location)      }  }   override func touchesended(touches: set<uitouch>, withevent event: uievent?) {      touch in touches {          let location = touch.locationinnode(self)          // deregistering unnecessary finger tracks         self.fingertracks.removeatindex(self.getclosestfingerarrayindexbylocation(location))      } }   override func touchescancelled(touches: set<uitouch>?, withevent event: uievent?) {      // touchescancelled occurs e.g. when more 5 fingers on iphone on screen      // deregistering tracks here     self.fingertracks.removeall()  }   override func update(currenttime: nstimeinterval) {      // see in console happening in array     if fingertracks.count > 0 {          fingertrack in self.fingertracks {              print("finger: \(fingertrack.fingerid) location: \(fingertrack.lastlocation)")         }      } else {          print("---")      } }   /*  * below helper functions convenience  */  func getclosestfingerarrayindexbylocation (location: cgpoint) -> int {      var fingertrackkeytoupdate : int = 0     var shortestdistance : cgfloat = 100500 // big number bigger screen size      (fingertrackkey, fingertrack) in self.fingertracks.enumerate() {          // calculating distance previous record each finger         let calculateddistance = sqrt(pow(fingertrack.lastlocation.x - location.x,2) + pow(fingertrack.lastlocation.y - location.y,2))          // shortest 1 gives finger id         if calculateddistance < shortestdistance {              shortestdistance = calculateddistance              fingertrackkeytoupdate = fingertrackkey         }      }      return fingertrackkeytoupdate }   func updatefingertracks(location: cgpoint) {      let closestfingerarrayindex = self.getclosestfingerarrayindexbylocation(location)     let fingeridtoupdate = self.fingertracks[closestfingerarrayindex].fingerid      self.fingertracks[closestfingerarrayindex] = fingertrackdata(fingerid: fingeridtoupdate, lastlocation: location)  }   func getclosestfingeridbylocation(location: cgpoint) -> int {      return self.fingertracks[self.getclosestfingerarrayindexbylocation(location)].fingerid  }   func getavailablefingerid() -> int {      var availablefingerid : int = 0      if self.fingertracks.count > 0 {          var checkagain = true          while checkagain {              checkagain = false              fingertrack in self.fingertracks {                 if availablefingerid == fingertrack.fingerid {                     checkagain = true                     availablefingerid++                 }             }         }     }      return availablefingerid }  } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -