使用 swift - IOS 接触多个节点
touching multiple nodes with swift - IOS
编辑
为了解决我的问题,我最初忘记将此代码包含到我的代码中,我将它放在这里以供以后遇到相同问题的任何人使用。
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
}
}
}
此代码不允许同时进行两次触摸。
有什么我想念的吗?
您可能忘记了在视图中启用多点触控?
When set to YES, the receiver receives all touches associated with a multi-touch sequence. When set to NO, the receiver receives only the first touch event in a multi-touch sequence. The default value of this property is NO.
编辑 为了解决我的问题,我最初忘记将此代码包含到我的代码中,我将它放在这里以供以后遇到相同问题的任何人使用。
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
}
}
}
此代码不允许同时进行两次触摸。 有什么我想念的吗?
您可能忘记了在视图中启用多点触控?
When set to YES, the receiver receives all touches associated with a multi-touch sequence. When set to NO, the receiver receives only the first touch event in a multi-touch sequence. The default value of this property is NO.