【问题标题】:SpriteKit tracking multiple touchesSpriteKit 跟踪多次触摸
【发布时间】:2016-07-03 20:22:28
【问题描述】:

我有几个按钮可以移动主角。 (左,右,跳跃等) 但是,当我一次触摸多个按钮时,前一个按钮就结束了。我的问题是,我如何在触摸期间保持两个触摸都保持活力?例如,这将允许角色同时向前移动和跳跃。我已将 multipleTouchEnabled 设置为 true。我读过使用字典来跟踪触摸会有所帮助,但我似乎无法完全理解实现。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let location = touch.locationInView(nil)

            if location.x < self.size.width / 2 && location.y > self.size.height / 2 {
                movingLeft = true
            }
            if location.x > self.size.width / 2 && location.y > self.size.height / 2 {
                movingRight = true
            }
            if location.x < self.size.width / 2 && location.y < self.size.height / 2 {
                jump()
            }
            if location.x > self.size.width / 2 && location.y < self.size.height / 2 {
                jump()
            }
        }
    }

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        movingLeft = false
        movingRight = false
    }

func jump() {
    mainCharacter.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
    mainCharacter.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 400))
}

【问题讨论】:

    标签: swift sprite-kit touchesbegan


    【解决方案1】:

    逻辑

    您应该创建一个活动触摸字典。

    private var activeTouches = [UITouch:String]()
    

    每次触摸开始时,您都将其保存到字典中并为其分配一个标签。

    activeTouches[touch] = "left"
    

    因此,当触摸结束时,您可以在字典中搜索它并找到相关标签。现在您知道用户释放了哪个按钮。

    let button = activeTouches[touch]
    if button == "left" { ... }
    

    别忘了从字典中删除它。

    activeTouches[touch] = nil
    

    实现

    class GameScene: SKScene {
    
        private var activeTouches = [UITouch:String]()
    
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            for touch in touches {
                let button = findButtonName(from:touch)
                activeTouches[touch] = button
                tapBegin(on: button)
            }
        }
    
        override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
            for touch in touches {
                guard let button = activeTouches[touch] else { fatalError("Touch just ended but not found into activeTouches")}
                activeTouches[touch] = nil
                tapEnd(on: button)
            }
        }
    
        private func tapBegin(on button: String) {
            print("Begin press \(button)")
            // your custom logic goes here
        }
    
        private func tapEnd(on button:String) {
            print("End press \(button)")
            // your custom logic goes here
        }
    
    
        private func findButtonName(from touch: UITouch) -> String {
            // replace this with your custom logic to detect a button location
            let location = touch.locationInView(self.view)
            if location.x > self.view?.frame.midX {
                return "right"
            } else {
                return "left"
            }
        }
    }
    

    在上面的代码中你应该把你自己的代码放进去

    1. tapBegin:这个方法接收一个按钮的标签并开始一些动作。

      例如开始运行。

    2. tapEnd:此方法接收按钮的标签并停止某些操作。

      例如停止运行。

    3. findButtonName:该方法接收一个UITouch,并返回用户按下的按钮的标签。

    测试

    我在 iPhone 上测试了之前的代码。我执行了以下操作。

    1. 开始按下屏幕的右侧
    2. 开始按下屏幕的左侧
    3. 从屏幕的右侧移除手指
    4. 从屏幕的左侧移除手指

    正如您在以下日志中看到的,代码能够识别不同的触摸

    Begin press right
    Begin press left
    End press right
    End press left
    

    结论

    我希望我说清楚了。如果有问题请告诉我。

    【讨论】:

    • 为什么要将触摸存储在字典中?如果我删除该代码,它仍然有效。
    猜你喜欢
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多