如何在swift Spritekit中限制touchesBegan中的触摸次数

时间:2016-01-21 17:30:35

标签: xcode swift touchesbegan

我在touchesBegan函数中为触摸点编写了一个位置查找器,我想将视图控制器中允许的触摸点数量限制为2,但我不太清楚如何做到这一点。一点帮助就太棒了。

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


    Object.physicsBody?.affectedByGravity = true
    Object2.physicsBody?.affectedByGravity = true
    Object3.physicsBody?.affectedByGravity = true


    if Object.containsPoint(location) {
  Object.physicsBody?.velocity = CGVectorMake(0, 0)
    Object.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 135))

}

        if Object2.containsPoint(location) {
           Object2.physicsBody?.velocity = CGVectorMake(0, 0)
            Object2.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 135))
 }


        if Object3.containsPoint(location) {
            Object3.physicsBody?.velocity = CGVectorMake(0, 0)
            Object3.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 135))



    }

    }


        }

1 个答案:

答案 0 :(得分:2)

如您所见touches是一个Set结构对象,并且它具有基数,在本例中为touches.count所以重点是找到这个基数,将其与{{1只有当它小于或等于2时才执行操作。

所以它就像这样

2