swift - 为什么我的 SpriteKit 对象不正确地碰撞?
问题描述
我正在尝试按照本教程学习 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)
}
}
}
}
我怎样才能得到我的预期行为?谢谢你。
解决方案
推荐阅读
- ios - 从 USDZ 文件转换为 SceneKit(SCN) 文件格式后,对象显示为黑色
- java - 如何修复 Android 中的 For 循环和随机问题?
- typeorm - 错误:添加多对多关系后未找到对象元数据
- hyperledger-fabric - Hyperledger Fabric 1.4 私有数据收集
- javascript - 如何计算动态创建的输入值的总和?
- reactjs - 向 ReactJS Webapp 添加唯一标识符
- python - 不同大小的工艺线到csv
- c# - 谁字段未更新 c# 多米诺日历创建
- python-3.x - 清除 tkinter 画布的问题
- qemu - 如何检测 AVX512 指令在 Qemu 上执行?