我在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))
}
}
}
答案 0 :(得分:2)
如您所见touches
是一个Set
结构对象,并且它具有基数,在本例中为touches.count
所以重点是找到这个基数,将其与{{1只有当它小于或等于2
时才执行操作。
所以它就像这样
2