我知道如何检测联系人中何时有两个对象,并且我知道如何检测屏幕何时被触摸。但是,如果我想知道联系人中有两个对象时是否已触摸屏幕,该怎么办?如果触摸在接触之前(而不是在接触期间)进行,touchesBegan上的布尔标志将起作用。
var screenTouch = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for _ in touches {
screenTouch = true
}
}
func didBegin(_ contact: SKPhysicsContact) {
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch collision {
case PhysicsCategories.Ball | PhysicsCategories.Edge:
if screenTouch {
print("LAUNCH!")
}
etc.
最佳答案
您必须创建一个条件变量,当didBegin
时为true,当didEnd
时为false。在条件变量为true的情况下,在touchesBegin
中完成操作。
var yourBodiesInContact = false
func didBegin(_ contact: SKPhysicsContact) {
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.Ball | PhysicsCategory.Edge {
yourBodiesInContact = true
}
}
func didEnd(_ contact: SKPhysicsContact) {
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.Ball | PhysicsCategory.Edge {
yourBodiesInContact = false
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if yourBodiesInContact {
// while in contact
} else {
// ...
}
}
关于ios - SpriteKit touchesBegan和didBeginContact,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44158325/