EDIT为了解决我的问题,我忘记了最初将此代码包含在我的代码中,我稍后将其放在这里供任何有相同问题的人使用。
override func didMoveToView(view: SKView) {
self.view?.multipleTouchEnabled = true // --------- set multiple touch
}
我正在制作一款带有 3 个按钮(节点(、一个向左、向右移动的按钮和一个满足我所有拍摄需求的开火按钮的游戏。无论如何,我想让我的游戏同时识别射击和移动触摸。这是我所有的触摸代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let touches = self.nodeAtPoint(location)
if (touches.name == "rightBtn") {
rightDown = true
}
if (touches.name == "leftBtn") {
leftDown = true
}
if (touches.name == "fireBtn") {
shooting = true
}
if (touches.name == "fireBtn" && touches.name == "rightBtn") {
rightDown = true
shooting = true
}
if (touches.name == "fireBtn" && touches.name == "leftBtn") {
leftDown = true
shooting = true
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let touches = self.nodeAtPoint(location)
let prevLocation = touch.previousLocationInNode(self)
let prevLocationTouches = self.nodeAtPoint(prevLocation)
if (touches.name == "rightBtn" && prevLocationTouches.name == "rightBtn") {
rightDown = true
leftDown = false
} else if (touches.name == "leftBtn" && prevLocationTouches.name == "leftBtn") {
rightDown = false
leftDown = true
} else {
rightDown = false
leftDown = false
}
if (touches.name == "fireBtn" && prevLocationTouches.name == "fireBtn") {
shooting = true
}
if (touches.name == "fireBtn" && prevLocationTouches.name == "rightBtn") {
shooting = true
rightDown = true
}
if (touches.name == "fireBtn" && prevLocationTouches.name == "leftBtn") {
leftDown = true
shooting = true
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let touches = self.nodeAtPoint(location)
if (touches.name == "rightBtn") {
rightDown = false
} else if (touches.name == "leftBtn") {
leftDown = false
} else {
shooting = false
}
}
}
此代码不允许同时触摸两次。我错过了什么吗?
也许您忘记在视图中启用多个触摸?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled
设置为 YES 时,接收器将接收与多点触控序列关联的所有触摸。设置为 NO 时,接收器仅接收多点触摸序列中的第一个触摸事件。此属性的默认值为 NO。