【问题标题】:Swift Spritekit count touchesSwift Spritekit 计数触摸
【发布时间】:2015-07-14 21:54:16
【问题描述】:

我正在尝试在一个节点上执行多个操作。

例如,在第一次触摸该节点时,第一个动作应该运行。在第二次触摸时:第二个动作应该运行。

下面是带有 touches.count 的代码无效示例。

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)

        if node == myNode {
            if touches.count == 1 {
                action1()
            }
            if touches.count == 2 {
                action2()
            }
            if touches.count == 3 {
                action3()
            }
        }
    }
}

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    您需要一个成员变量来跟踪应用程序启动后的触摸次数。 touches.count 方法不是累积的。

    var cumulativeNumberOfTouches = 0
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let node = self.nodeAtPoint(location)
    
            if node == myNode {
    
                cumulativeNumberOfTouches += 1
    
                switch cumulativeNumberOfTouches {
                case 1:
                    action1()
                case 2:
                    action2()
                case 3:
                    action3()
                default:
                    /* do something or nothing or whatever */
                    println("\(cumulativeNumberOfTouches) touches")
                }
            }
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我认为您想使用 touches.tapCount 而不是计数。这会检测到您正在执行的敲击,并应记录 1、2 和 3(敲击速度很快,因此您必须快速敲击)。

      编辑:您是指有人点击屏幕多少次,还是指一次有多少手指在屏幕上?

      【讨论】:

      • tapCount 也不是解决方案。这计算了一个“触摸阶段”中的计数次数
      猜你喜欢
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      相关资源
      最近更新 更多