swift Spritekit中touchesBegan如何限制触摸次数
How to limit the number of touches in touchesBegan in swift Spritekit
我在 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))
}
}
}
如你所见,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
}
}
}
我在 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))
}
}
}
如你所见,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
}
}
}