【发布时间】:2015-06-14 14:50:23
【问题描述】:
在我正在创建的 OSX 游戏中,didBeginContact 内部的逻辑存在一些问题。它是用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 语句,或者是否会更有效。我已经尝试删除“else”并且只有一系列“if”语句,但是当发生碰撞时我会遇到更多错误(盒子从墙壁上掉下来等等。)任何建议都会受到欢迎
【问题讨论】:
标签: macos swift if-statement logic collision