我正在尝试学习this tutorial
之后的SpriteKit一切正常,直到我想在两个不同的对象之间添加另一个碰撞。我有敌人结点,玩家(飞船),弹丸和流星,敌人掉下来后,您将它们与弹丸从船上射击,避开了流星。我尝试设置物理类别结构,为每个节点设置正确的物理类别并创建碰撞函数。
问题是,当流星还击中敌人时,流星触发了应该发生的功能,当流星也击中敌人时,我试图改变事物,并且碰撞只是混合而已,但我永远不可能完全正确。我只希望射弹击中敌舰,而流星则击毁玩家的舰船。
这是我所做的:
struct PhysicsCategory {
static let none : UInt32 = 0
static let all : UInt32 = UInt32.max
static let monster : UInt32 = 0x2 // 1
static let projectile: UInt32 = 0x3 // 2
static let spaceship : UInt32 = 0x4
static let meteor : UInt32 = 0x5
static let edge : UInt32 = 0x6
}
宇宙飞船:
spaceship.physicsBody = SKPhysicsBody(rectangleOf: spaceship.size) // 1
spaceship.physicsBody?.isDynamic = true // 2
spaceship.physicsBody?.categoryBitMask = PhysicsCategory.spaceship // 3
spaceship.physicsBody?.contactTestBitMask = PhysicsCategory.meteor// 4
spaceship.physicsBody?.collisionBitMask = PhysicsCategory.none // 5
Enemy:
monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size) // 1
monster.physicsBody?.isDynamic = true // 2
monster.physicsBody?.categoryBitMask = PhysicsCategory.monster // 3
monster.physicsBody?.contactTestBitMask = PhysicsCategory.projectile // 4
monster.physicsBody?.collisionBitMask = PhysicsCategory.none // 5
弹丸:
projectile.physicsBody = SKPhysicsBody(rectangleOf: projectile.size)
projectile.physicsBody?.isDynamic = true
projectile.physicsBody?.categoryBitMask = PhysicsCategory.projectile
projectile.physicsBody?.contactTestBitMask = PhysicsCategory.monster
projectile.physicsBody?.collisionBitMask = PhysicsCategory.none
projectile.physicsBody?.usesPreciseCollisionDetection = true
流星:
monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size) // 1
monster.physicsBody?.isDynamic = true // 2
monster.physicsBody?.categoryBitMask = PhysicsCategory.meteor // 3
monster.physicsBody?.contactTestBitMask = PhysicsCategory.none // 4
monster.physicsBody?.collisionBitMask = PhysicsCategory.none// 5
最后,物理功能:
func projectileDidCollideWithMonster(projectile: SKSpriteNode, monster: SKSpriteNode) {
print("Hit")
run(SKAction.playSoundFileNamed("Dull-Impact-Hit.wav", waitForCompletion: false))
hits += 1
scoreLabel.text = "Score: \(hits)/25"
if hits == 25 {
self.removeAction(forKey: "addMonster")
self.scoreLabel.text = "Victory!"
}
monster.removeAction(forKey: "spaceshipMoving")
monster.run(SKAction.animate(with: explosionAnimation,
timePerFrame: 0.1,
resize: false,
restore: true))
monster.run(SKAction.animate(with: explosionAnimation,
timePerFrame: 0.1,
resize: false,
restore: true)) {
monster.removeFromParent()
}
projectile.removeFromParent()
}
func meteorDidCollideWithSpaceship(meteor: SKSpriteNode, spaceship: SKSpriteNode) {
print("Ouch")
run(SKAction.playSoundFileNamed("Dull-Impact-Hit.wav", waitForCompletion: false))
health -= 1
healthLabel.text = String(health)
print("health" + String(health))
if health == 0 {
scoreLabel.text = "You died!"
healthLabel.text = "☠"
self.view?.isUserInteractionEnabled = false
self.spaceship.removeFromParent()
}
}
}
extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
// 1
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// 2
if ((firstBody.categoryBitMask & PhysicsCategory.monster != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.projectile != 0)) {
if let monster = firstBody.node as? SKSpriteNode,
let projectile = secondBody.node as? SKSpriteNode {
projectileDidCollideWithMonster(projectile: projectile, monster: monster)
}
}
if ((firstBody.categoryBitMask & PhysicsCategory.spaceship != 0 ) &&
(secondBody.categoryBitMask & PhysicsCategory.meteor != 0 )) {
if let meteor = firstBody.node as? SKSpriteNode,
let spaceship = secondBody.node as? SKSpriteNode {
meteorDidCollideWithSpaceship(meteor: meteor, spaceship: spaceship)
}
}
}
}
如何获得预期的行为?谢谢。