作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 SpriteKit 中构建一个游戏,玩家试图避免与物体发生碰撞。对象每 3 到 5 秒创建一次,我正试图找出一种方法,一旦它与屏幕底部发生碰撞,就从父对象中删除,但由于某种原因,当它发生时它会进入游戏结束请看下面的横向代码.
import SpriteKit
class PlayScene: SKScene, SKPhysicsContactDelegate {
enum ColliderType: UInt32 {
case none = 0
case player1 = 1
case object = 2
case border = 4
case remove = 8
}
gameOver = false
let ground = SKNode()
ground.position = CGPoint(x: 0, y: 0)
ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width + 500, height: 1))
ground.physicsBody?.isDynamic = false
ground.physicsBody?.categoryBitMask = ColliderType.remove.rawValue
ground.physicsBody?.contactTestBitMask = ColliderType.object.rawValue
ground.physicsBody?.collisionBitMask = ColliderType.none.rawValue
character1.physicsBody!.categoryBitMask = ColliderType.player1.rawValue
character1.physicsBody!.contactTestBitMask = ColliderType.object.rawValue
character1.physicsBody!.collisionBitMask = ColliderType.object.rawValue | ColliderType.border.rawValue
object1.physicsBody!.categoryBitMask = ColliderType.object.rawValue
object1.physicsBody!.contactTestBitMask = ColliderType.player1.rawValue | ColliderType.remove.rawValue
object1.physicsBody!.collisionBitMask = ColliderType.player1.rawValue | ColliderType.border.rawValue | ColliderType.object.rawValue
if contact.bodyA.categoryBitMask == ColliderType.object.rawValue || contact.bodyB.categoryBitMask == ColliderType.object.rawValue {
gameOver == true
}
if contact.bodyA.categoryBitMask == ColliderType.remove.rawValue || contact.bodyB.categoryBitMask == ColliderType.remove.rawValue {
object1.removeFromParent()
// it runs this code as well as code above collision object with object but i want contact remove with object
}
最佳答案
尝试在未来发布您的完整代码,而不是将一些随机的代码拼凑在一起。
您的碰撞代码没有多大意义。它应该看起来更像这样,你检查 player->object and object->remove 或者反之亦然
func didBegin(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
// This way you only need to do 1 if statement for each collision.
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// Player hit Object or Object hit Player
if (firstBody.categoryBitMask == ColliderType.player.rawValue) && (secondBody.categoryBitMask == ColliderType.object.rawValue) {
gameOver == true
}
// Object hit Remove or Remove hit Object
if (firstBody.categoryBitMask == ColliderType.object.rawValue) && (secondBody.categoryBitMask == ColliderType.remove.rawValue) {
// Remove object
firstBody.node?.removeFromParent()
}
}
我建议你阅读更多关于 sprite kit collision 的教程,如果这让你感到困惑,有很多可用的。
希望对你有帮助
关于ios - 如何在离开 SpriteKit 场景后删除 SKNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39968479/
我是一名优秀的程序员,十分优秀!