为了避免意外的游戏行为,我想知道在任何特定时刻触摸屏上所有手指的坐标。有没有办法做到这一点?
例如,当从屏幕上取下第二根手指时,获取第一根手指的坐标。
提前致谢!
答案 0 :(得分:0)
这是基于trojanfoe的想法的解决方案。
import SpriteKit
class GameScene : SKScene {
struct fingerTrackData {
var fingerID : Int // used to identify fingers in other part of the code
var lastLocation : CGPoint // location of each finger
}
// array that stores information about each finger coordinates
var fingerTracks : [fingerTrackData] = []
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
// getAvailableFingerID() returns available fingerID to avoid ID duplication
self.fingerTracks.append(fingerTrackData(fingerID: self.getAvailableFingerID(), lastLocation: location))
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
self.updateFingerTracks(location)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for 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 than 5 fingers on iPhone is on the screen
// Deregistering all tracks here
self.fingerTracks.removeAll()
}
override func update(currentTime: NSTimeInterval) {
// to see in console what is actually happening in array
if fingerTracks.count > 0 {
for fingerTrack in self.fingerTracks {
print("Finger: \(fingerTrack.fingerID) Location: \(fingerTrack.lastLocation)")
}
} else {
print("---")
}
}
/*
* Below are some helper functions for convenience
*/
func getClosestFingerArrayIndexByLocation (location: CGPoint) -> Int {
var fingerTrackKeyToUpdate : Int = 0
var shortestDistance : CGFloat = 100500 // some big number that is much bigger than screen size
for (fingerTrackKey, fingerTrack) in self.fingerTracks.enumerate() {
// calculating distance from previous record for each finger
let calculatedDistance = sqrt(pow(fingerTrack.lastLocation.x - location.x,2) + pow(fingerTrack.lastLocation.y - location.y,2))
// shortest one gives us 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
for fingerTrack in self.fingerTracks {
if availableFingerID == fingerTrack.fingerID {
checkAgain = true
availableFingerID++
}
}
}
}
return availableFingerID
}
}
答案 1 :(得分:0)
感谢大家的帮助!
class GameScene : SKScene {
// stores all touches at any moment
var touchTracks : [UITouch] = []
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
self.touchTracks.append(touch)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
var touchIndexToRemove : Int?
for i in 0..<self.touchTracks.count {
if self.touchTracks[i] == touch {
touchIndexToRemove = i
}
}
self.touchTracks.removeAtIndex(touchIndexToRemove!)
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
// touchesCancelled occurs e.g. when more than 5 fingers on iPhone is on the screen
// Deregistering all tracks here
self.touchTracks.removeAll()
}
}