2015-06-14 84 views
0

我在創建的OSX遊戲的didBeginContact裏面的邏輯有些麻煩。這是寫在迅速,這裏是代碼當前是:didBeginContact邏輯OSX swift

func didBeginContact(contact: SKPhysicsContact) { 

    let collision: UInt32 = contact.bodyA.categoryBitMask | 
     contact.bodyB.categoryBitMask 

    if collision == PhysicsCategory.player | PhysicsCategory.wall { 

    } else if collision == 
     PhysicsCategory.player | PhysicsCategory.box { 
      player.setJump(true) 

    } else if collision == 
     PhysicsCategory.player | PhysicsCategory.floor { 
      player.setJump(true) 
    } 

    if collision == 
     PhysicsCategory.laser | PhysicsCategory.wall { 

      effects = SKEmitterNode(fileNamed: "Explosion.sks") 
      addChild(effects!) 
      laser!.removeFromParent() 
    } 

    else if collision == 
     PhysicsCategory.laser | PhysicsCategory.floor { 
      effects = SKEmitterNode(fileNamed: "Explosion.sks") 
      addChild(effects!) 
      laser!.removeFromParent() 
    } 

我覺得這混亂的if語句是效率極其低下,但我不知道如何將其轉換爲一個switch語句或者是否會甚至更有效率。我嘗試刪除'其他',只是有一系列的'如果'的聲明,但當碰撞發生時,我還有更多的錯誤(盒子掉入牆壁等等。)任何建議將是令人滿意的

回答

0

如果有人絆倒了這一點,這是我如何解決它。

let collision: UInt32 = contact.bodyA.categoryBitMask | 

contact.bodyB.categoryBitMask 

    switch collision{ 

    case PhysicsCategory.player | PhysicsCategory.floor: 
     playerObj.setJump(true) 

    case PhysicsCategory.player | PhysicsCategory.box: 
     playerObj.setJump(true) 

    case PhysicsCategory.player | PhysicsCategory.wall: 
     return 
    // ect... 
    default: 
     return 
}