我正在开发一款游戏,它包含一些在场景中移动的箭头。我设置了一些碰撞,我需要移除与屏障相关的箭头。我设置的非常简单和功能,但只有一个箭头,因为它是数组的元素[0],显然如果另一个(不是[0])接触屏障,它将不会被删除来自父母。有什么办法可以用来将碰撞委托给所有元素,尽管需要手动赋予每个数字的数组元素吗?
// 3. react to the contact between the two nodes
if firstBody.categoryBitMask == barrier?.physicsBody?.categoryBitMask &&
secondBody.categoryBitMask == arrows[0].physicsBody?.categoryBitMask {
// Player & barrier
gameOver(false)
} else if firstBody.categoryBitMask == goal?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == arrows[0].physicsBody?.categoryBitMask {
// Player & Goal
arrows[0].removeFromParent()
// gameOver(true)
谢谢!
:)
答案 0 :(得分:1)
然后你初始化你的arrow
和goal
你应该给他们categoryBitMask
arrow.physicsBody?.categoryBitMask = 0x1 << 0
goal.physicsBody?.categoryBitMask = 0x1 << 1
barrie.physicsBody?.categoryBitMask = 0x1 << 2
另外,我建议您创建Constants.swift
文件并将常量存储在那里。
Constants.swift
:
let kCategoryArrow: UInt32 = 0x1 << 0
let kCategoryGoal: UInt32 = 0x1 << 1
let kCategoryBarrie: UInt32 = 0x1 << 2
然后你可以像这样给categoryBitMask
(然后你初始化它们)
arrow.physicsBody?.categoryBitMask = kCategoryArrow
goal.physicsBody?.categoryBitMask = kCategoryGoal
barrier.physicsBody?.categoryBitMask = kCategoryBarrier
最后检查联系方式:
/* Arrow vs Barrier */
if bodyA.physicsBody?.categoryBitMask == kCategoryArrow && bodyB.physicsBody?.categoryBitMask == kCategoryBarrier {
gameOver(false)
} else if bodyA.physicsBody?.categoryBitMask == kCategoryBarrier && bodyB.physicsBody?.categoryBitMask == kCategoryBullet {
gameOver(false)
}
/* Arrow vs Goal */
if bodyA.physicsBody?.categoryBitMask == kCategoryArrow && bodyB.physicsBody?.categoryBitMask == kCategoryGoal {
bodyA.node.removeFromParent()
} else if bodyA.physicsBody?.categoryBitMask == kCategoryGoal && bodyB.physicsBody?.categoryBitMask == kCategoryBullet {
bodyB.node.removeFromParent()
}