【问题标题】:didBegin is not called没有调用 didBegin
【发布时间】:2018-12-02 12:31:25
【问题描述】:

我的目标是输出 "hit",但不改变cardanotherCard 的位置。他们应该互相接触,但不能移动。但是没有调用 didBegin。

结构:

struct physicBodyCharacters {

    static let cardNumber = 00000001 //1
    static let anotherCardNumber = 00000010 //2
    static let nobodyNumber = 00000100 //4
}

在 viewDidLoad() 中:

gameScene2.physicsWorld.gravity = CGVector(dx: 0, dy: -9.81)
    gameScene2.physicsWorld.contactDelegate = self

第一个节点:

card = SKSpriteNode(texture: cardTexture)
    card.position = CGPoint(x: gameScene2.size.width / 2 + 150, y: 95)
    card.zPosition = 3
    card.setScale(1)
    card.physicsBody = SKPhysicsBody(texture: cardTexture, size: card.size)
    card.physicsBody?.affectedByGravity = false
    card.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.cardNumber)
    card.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
    card.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.anotherCardNumber)

第二个节点:

anotherCard = SKSpriteNode(texture: anotherCardTexture)
    anotherCard.position = CGPoint(x: 31 , y: 532)
    anotherCard.zPosition = 2
    anotherCard.setScale(1)
    anotherCard.physicsBody = SKPhysicsBody(texture: anotherCardTexture, size: battlefieldCard0.size)
    anotherCard.physicsBody?.affectedByGravity = false
    anotherCard.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.anotherCardNumber)
    anotherCard.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
    anotherCard.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.cardNumber)

didBegin() 函数:

func didBegin(_ contact: SKPhysicsContact) {
    print("contact")
    let contanctMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch contanctMask
    {
    case UInt32(physicBodyCharacters.cardNumber) | UInt32(physicBodyCharacters.anotherCardNumber):
        print("hit")
    default:
        break
    } 
}

对于每一个回答我都非常感谢。

【问题讨论】:

  • 您没有显示所有相关的代码行。
  • 假设您在 SKScene 中声明了此方法,“请确保在需要检测联系人之前在某处写入“self.physicsWorld.contactDelegate = self”,否则不会调用您的联系人方法。
  • self.physicsWorld.contactDelegate = self 已经在我的代码中。问题一定出在其他地方。

标签: swift sprite-kit


【解决方案1】:

要在 2 个节点接触时收到通知,但不让它们移动,那么您需要在节点之间打开接触检测(调用 didBegin)但关闭碰撞检测(因为它是碰撞使节点在接触时移动)。

这是通过正确设置collisionBitMaskcontactTestBitMask 来完成的。

您没有发布足够多的代码供我们检查,但您可能需要通读以下关于其他类似问题的答案:

碰撞和接触的分步指南: https://stackoverflow.com/a/51041474/1430420

以及碰撞和接触测试位掩码指南: https://stackoverflow.com/a/40596890/1430420

操作位掩码以关闭和打开单个碰撞和接触。 https://stackoverflow.com/a/46495864/1430420

编辑:

我认为您的类别定义是错误的 - 它们应该是二进制位掩码,但您已将它们定义为小数。

尝试将它们的定义更改为:

struct physicBodyCharacters {

static let cardNumber = 00000001 << 0 // 1
static let anotherCardNumber = 00000010 << 1 // 2
static let nobodyNumber = 00000100 << 2 // 4

【讨论】:

  • 我已阅读文章并尝试在某些地方重写我的代码。但是,同样的问题仍然存在。你能帮我解决这个问题吗?
  • 您没有列出很多代码。例如,您定义物理体的代码、类别位掩码等。如果您更改了代码,请将新代码添加到问题中。
  • 我现在添加了更多代码。请再看一遍好吗?
  • 我改成二进制位掩码了,还是不行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多