繁体   English   中英

Swift Spritekit计数功能

[英]Swift Spritekit count touches

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

因此,例如,在此节点的第一次触摸上,第一个操作应运行。 第二点:第二个动作应该运行。

下面是带有touchs.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()
            }
        }
    }
}

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

编辑:您的意思是某人点击屏幕多少次,或者一次屏幕上有多少手指?

您需要一个成员变量来跟踪自应用启动以来的触摸次数。 方法touchs.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")
            }
        }
    }

}

暂无
暂无

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

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