2017-05-24 10 views
0

私は連絡先の2つのオブジェクトを検出する方法を知っており、画面がタッチされたときを検出する方法を知っています。しかし、連絡先の2つのオブジェクトの間に画面がタッチされたかどうかを知りたければどうなりますか? touchesBeganのブール値フラグは、接触が連絡先の前に来た場合に機能しますが、その間には受け付けません。SpriteKit touchesBeganとdidBeginContact

あなたはとき didBeginと偽 didEnd真である条件変数を作成する必要が
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. 

答えて

1

。そして、あなたの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 { 
     // ... 
    } 
} 
関連する問題