gpt4 book ai didi

swift - collisionBitMask 值在 SpriteKit 中不起作用

转载 作者:行者123 更新时间:2023-11-28 12:18:47 24 4
gpt4 key购买 nike

我有两个实体想要相互碰撞。我有一个结构来跟踪不同的物理类别:

struct PhysicsCategory {
static let Player: Int32 = 0x1 << 1
static let Obstacle: Int32 = 0x1 << 2
static let Ground: Int32 = 0x1 << 3
}

我希望我的 Player 节点与我的 Obstacle 节点发生碰撞。这是我的播放器节点的物理特性:

    self.physicsBody = SKPhysicsBody(rectangleOf: (self.size))
self.physicsBody?.isDynamic = true
self.physicsBody?.categoryBitMask = UInt32(PhysicsCategory.Player)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)

这是我的 Obstacle 节点的物理特性

    self.physicsBody = SKPhysicsBody(rectangleOf: self.size)
self.physicsBody?.categoryBitMask = UInt32(PhysicsCategory.Obstacle)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)
self.physicsBody?.isDynamic = true
self.physicsBody?.velocity = CGVector(dx: -240, dy: 0)
self.physicsBody?.linearDamping = 0
self.physicsBody?.friction = 0

当它们相交时,它们只是彼此擦肩而过。但是,它们都正确地与地面碰撞。我做错了什么?

最佳答案

第二次设置时,您正在覆盖 collisionBitMask。您应该使用符号 | 设置一个 OR 位掩码。

替换

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)

与:

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle) | UInt32(PhysicsCategory.Ground)

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)

与:

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player) | UInt32(PhysicsCategory.Ground)

关于swift - collisionBitMask 值在 SpriteKit 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45510159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com