ios - 为什么这些碰撞没有感觉? ( swift SpriteKit )

标签 ios swift xcode sprite-kit physics

在我的项目中,我试图检测两个 SKSpriteNode 之间的碰撞,但它不起作用。 这是我的物理类别代码 :

struct PhysicsCategory {
static let enemy : UInt32 = 0x1 << 1
static let player : UInt32 = 0x1 << 3
static let neutral : UInt32 = 0x1 << 2
 }

对于中立者、敌人和玩家 body

  neutral = SKSpriteNode(imageNamed: "Cave-Bottom")
    neutral.size = CGSize(width: self.frame.width, height: self.frame.height / 8)
    neutral.position = CGPoint(x: 0, y: self.frame.height / -2.5)
    neutral.zPosition = 2
    neutral.physicsBody?.categoryBitMask = PhysicsCategory.neutral
    neutral.physicsBody?.collisionBitMask = PhysicsCategory.player
    neutral.physicsBody?.contactTestBitMask = PhysicsCategory.player
    neutral.physicsBody = SKPhysicsBody.init(rectangleOf:              neutral.size)

    enemy = SKSpriteNode(imageNamed: "spike")
    enemy.size = CGSize(width: 40, height: 40)
    enemy.position = CGPoint(x: 0, y: 0)
    enemy.zPosition = 2
    enemy.physicsBody = SKPhysicsBody.init(rectangleOf: enemy.size)
    enemy.physicsBody?.categoryBitMask = PhysicsCatagory.enemy
    enemy.physicsBody?.collisionBitMask = PhysicsCatagory.player
    enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.player

    player = SKSpriteNode(imageNamed: "bob")
    player.size = CGSize(width: 40, height: 40)
    bob.zPosition = 3
    player.physicsBody?.categoryBitMask = PhysicsCatagory.player
    player.physicsBody?.collisionBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
    player.physicsBody?.contactTestBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
    player.physicsBody = SKPhysicsBody.init(rectangleOf: player.size)

以及我的 ContactHasBegan 函数的代码:

  func didBegin(_ contact: SKPhysicsContact) {

   print("collided!")

    if (contact.bodyA.categoryBitMask == PhysicsCatagory.enemy && contact.bodyB.categoryBitMask == PhysicsCatagory.player) {
        print("player and enemy!")
    }
    if (contact.bodyB.categoryBitMask == PhysicsCatagory.enemy && contact.bodyA.categoryBitMask == PhysicsCatagory.player) {
        print("player and enemy!")
    }
    if (contact.bodyA.categoryBitMask == PhysicsCatagory.neutral && contact.bodyB.categoryBitMask == PhysicsCatagory.player) {
        print("neutral and player!")    
    }
    if contact.bodyB.categoryBitMask == PhysicsCatagory.neutral && contact.bodyA.categoryBitMask == PhysicsCatagory.player{
        print("neutral and player")
    }          }

出于某种原因,它只检测玩家和敌人之间的碰撞并打印“Collided!”并且 didBeginContact 函数中的所有 if 语句都没有测试出阳性。

最佳答案

因为玩家的 physicsBody 没有正确设置它的 categorycontactTest 位掩码。

在尝试设置玩家的 physicsBody 属性后,您已经创建了玩家的 physicsBody:

player.physicsBody?.categoryBitMask = PhysicsCatagory.player
player.physicsBody?.collisionBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
player.physicsBody?.contactTestBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
player.physicsBody = SKPhysicsBody.init(rectangleOf: player.size)

在前 3 行代码中,因为 player.physicsBody 是可选的,所以该行的其余部分将被忽略,因为没有 physicsBody。

因此创建的物理体的所有属性都设置为默认值,即它与所有物体碰撞但不接触任何物体。

你需要移动线:

player.physicsBody = SKPhysicsBody.init(....

在设置物理体属性的代码之前:

player.physicsBody = SKPhysicsBody.init(rectangleOf: player.size)
player.physicsBody?.categoryBitMask = PhysicsCatagory.player
player.physicsBody?.collisionBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
player.physicsBody?.contactTestBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral

关于ios - 为什么这些碰撞没有感觉? ( swift SpriteKit ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449413/

相关文章:

Xcode tableview 如何在每个部分中选择一行

swift - 主应用程序包与其包含的框架之间进行通信

ios - 使用 SwiftUI 如何强制 DatePicker 使用 24 小时日期格式而不是用户设备上的区域设置

java - 没有收到来自 firebase 的推送通知但是使用 Pusher 应用程序

iphone - UIPickerView根据数据显示数据

swift - 如何获取 JSON 格式的 Swift REPL 编译器错误

swift - 向 Google map 上的 KML 图层添加颜色

ios - 如何保护 AWS REST 后端以进行移动访问

objective-c - 表格 View 中的完整文件列表

arrays - Swift - 将数组写入文本文件