ios - 快速接触多个节点 - IOS

标签 ios iphone swift multi-touch simultaneous

编辑 为了解决我的问题,我最初忘记将这段代码包含到我的代码中,我将它放在这里供以后遇到相同问题的任何人使用。

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
        }

    }
}

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

最佳答案

也许您忘记了在 View 中启用多点触控?

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

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.

关于ios - 快速接触多个节点 - IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487627/

相关文章:

iphone - Xcode没有编译任何项目? 'clang failed with exit code 255'

iphone - 如何制作两个像 2 行 uipickerview 一样工作的自定义表格 View

ios - CoreData 与 Swift 和 iOS 8+ 仍然需要 willAccessValueForKey()?

ios - Swift改变了一个 block 的参数类型,这也是Objective-C方法的参数

iphone - iPhone 上的 Lua

xcode - 在 MainMenu.xib 上将 NSViewController 作为工作表打开

ios - 以编程方式设置按钮背景图像 iPhone

ios - 在 iOS 中使用 NSJSONSerialization 进行 JSON 解析

ios - "Expected expression in Swift key path"使用扩展重构代码时出错

objective-c - UIBezierPath - 动画填充