繁体   English   中英

快速触摸多个节点-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
        }

    }
}

此代码不允许同时触摸两次。 有什么我想念的吗?

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM