【问题标题】:Sprite Kit - How to delay touchesBeganSprite Kit - 如何延迟 touchesBegan
【发布时间】:2016-01-03 07:11:49
【问题描述】:

我的应用程序的控件很简单。

  • 触摸屏幕左侧调用一项功能。
  • 触摸右侧调用第二个函数。 T
  • 同时触摸两侧调用第三个函数。

问题是,除非您同时触摸屏幕的右侧和左侧,否则将调用最先触摸的那一侧的函数。我希望能够等待几分之一秒,看看用户是否要在允许调用左侧或右侧功能之前触摸屏幕的两侧。

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 {
                touchingLeft = true
            }
            if location.x > self.size.width / 2 {
                touchingRight = true
            }

            if touchingRight && touchingLeft {
                ship.fireMain()

            //Wait for 1/10 of a second...

            } else if touchingLeft && !touchingRight {
                ship.fireLeft()
            } else if !touchingLeft && touchingRight {
                ship.fireRight()
            }
        }  
    }

【问题讨论】:

    标签: sprite-kit timestamp touchesbegan


    【解决方案1】:

    要检测左、右和左+右触摸,只需在短暂延迟后执行触摸测试即可。这是一个如何做到这一点的示例。

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let location = touch.locationInView(nil)
    
            if !touchingLeft && !touchingRight {
                // Adjust the delay as needed
                let delay = SKAction.waitForDuration(0.01)
                let test = SKAction.runBlock {
                    // Avoid retain cycle
                    [weak self] in
                    self?.testTouches()
                }
                let delayAndTest = SKAction.sequence([delay, test])
                runAction(delayAndTest)
            }
    
            if location.x < self.size.width / 2 {
                touchingLeft = true
            }
            else if location.x > self.size.width / 2 {
                touchingRight = true
            }
        }
    }
    
    func testTouches() {
        if touchingLeft && touchingRight {
            print("left+right")
        }
        else if touchingRight {
            print("right")
        }
        else {
            print("left")
        }
        touchingRight = false
        touchingLeft = false
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多