我的应用有多个球,但一次只能移动。我的以下代码显示,在touchesBegin时,我设置了touchesMoved中使用的特定字段。在touchesCancelled和touchesEnded中,我重置了所有内容。
这是我的第一个Swift应用程序,但不是我的第一个程序,我知道用户喜欢尝试使应用程序崩溃。因此,我用两根手指开始尽可能快地触摸多个球,最终,是的,我在第一个可以设置为“移动”球之前,先在另一个球上碰过touches。是否有任何滞后属性可以让iOS在touchesBegan之间等待甚至0.3秒?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let touchedNodes = self.nodes(at: location)
for node in touchedNodes{
if let theNode = node as? MyBall {
if theNode.nodeType == .ball, theNode.isMoving == false,
(myGV.currentBall == nil) {
theNode.isMoving = true
theNode.zPosition += 1
myGV.currentBall = theNode
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
guard touches.first != nil else { return }
if let touch = touches.first, let node = myGV.currentBall, node.isMoving == true {
let touchLocation = touch.location(in: self)
node.position = touchLocation
node.isMoving = true
node.inSlot = false
}
}
我认为自然的事情是围绕一种个体的主动接触进行组织。在touchesMoved
,,touchesEnded
和中touchesCancelled
,当没有活动触摸时,将忽略所有内容,并忽略不是活动触摸的所有触摸。在中touchesBegan
,如果已经有活动的触摸,请忽略新的触摸。最后,在中touchesBegan
,如果没有活动的触摸并且该触摸在一个球上,则该触摸变为活动的触摸,并且该球成为被移动的球。草图:
// activeTouch is a UITouch?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if activeTouch == nil {
// Look for a touched ball; if found set that as the moving ball
// and set this touch as activeTouch
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
guard let active = activeTouch else { return }
for touch in touches {
if touch == active {
// Do stuff with the moving ball
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
guard let active = activeTouch else { return }
for touch in touches {
if touch == active {
// The moving ball stops moving, reset activeTouch to nil
}
}
}
// Probably touchesCancelled should be the same as touchesEnded, or
// maybe you'd want to move the ball back to the starting position;
// depends on your game.
我以为是那样的。多亏了您的简化模板,我才发现自己在搞乱。谢谢