【问题标题】:How to limit the number of touches in touchesBegan in swift Spritekit如何在swift Spritekit中限制touchesBegan中的触摸次数
【发布时间】:2016-01-21 17:30:35
【问题描述】:

我在 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))



    }

    }


        }

【问题讨论】:

标签: xcode swift touchesbegan


【解决方案1】:

如你所见touches 是一个Set 结构对象,它有一个基数,在这种情况下是touches.count 所以关键是要找到这个基数,将它与2 进行比较,并且只有当它小于或等于2

原来是这样的

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if touches.count <= 2 {
        for touch: AnyObject in touches {
            // do stuff
            }
        }
    }

【讨论】:

  • @Xcoder555 如果您觉得有帮助,请点赞或标记为答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多