用swift触摸多个节点 - IOS

时间:2015-12-28 04:19:39

标签: ios iphone swift multi-touch simultaneous

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
        }

    }
}

此代码不允许同时进行两次触摸。 我有什么遗失的吗?

1 个答案:

答案 0 :(得分:2)

也许您忘记在视图中启用多个触摸?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled

  

当设置为YES时,接收器接收与多点触摸序列相关的所有触摸。当设置为“否”时,接收器仅接收多点触摸序列中的第一触摸事件。此属性的默认值为NO。