简体   繁体   English

节点在另一个Node的顶部,都有物理身体,只想检测大多数顶级物理体 - SpriteKit / Swift

[英]Node on top of another Node, both with Physics Bodies, Only want to detect most top physics body - SpriteKit / Swift

Creating a game in Swift and SprieKit. 在Swift和SprieKit中创建游戏。

I have 2 nodes, a BridgeNode and WaterNode. 我有2个节点,一个BridgeNode和WaterNode。 One on top of the other. 一个在另一个之上。

Both have physics bodies to detect when the player is either on the bridge on in the water. 两者都有物理机构来检测玩家何时在水中的桥上。 Both nodes are added independently as child nodes of the Scene. 两个节点都独立添加为场景的子节点。

When the player node jumps onto the Bridge, DidBegin detects contact with both the Water and Bridge nodes. 当玩家节点跳转到Bridge时,DidBegin会检测到与Water和Bridge节点的联系。 I only want it to detect the Bridge node as the player is safely on the Bridge OR if the player is in the water. 我只希望它能够检测到Bridge节点,因为玩家安全地在桥上或者玩家在水中。

func didBegin(_ contact: SKPhysicsContact) {
       // Did Begin Contact - Contact Testing and actions
       let player1 = (contact.bodyA.categoryBitMask == player1Mask) ? contact.bodyA : contact.bodyB
       let other = (player1 == contact.bodyA) ? contact.bodyB : contact.bodyA

    if other.categoryBitMask == bridgeMask {
        print("BRIDGE CONTACT")

    }
    else if other.categoryBitMask == waterMask {
        // Contacted Water
        print("WATER CONTACT")

    }
}

The console is printing both print statements always in a random order. 控制台始终以随机顺序打印两个打印语句。

Hope someone can help me to just detect one or the other. 希望有人可以帮我检测一个或另一个。

You mentioned that it is a top-down game, so when you have the bridge on top of the water the player will obviously contact both at the same time, there is no "blocking" of the physicsBody underneath the bridge. 你刚才提到,这是一个自上而下的游戏,所以当你在水面上的球员显然会在同一时间接触两个顶部的桥,没有桥下的physicsBody的“堵”。 You need to do something like this in your SKPhysicsContactDelegate : 你需要做这样的事情在你的SKPhysicsContactDelegate

var playerIsOnBridge = false

func didBegin(_ contact: SKPhysicsContact) {
    let player1 = (contact.bodyA.categoryBitMask == player1Mask) ? contact.bodyA : contact.bodyB
    let other = (player1 == contact.bodyA) ? contact.bodyB : contact.bodyA

    if other.categoryBitMask == bridgeMask || playerIsOnBridge {
        playerIsOnBridge = true
        print("BRIDGE CONTACT")
    } else if other.categoryBitMask == waterMask {
        print("WATER CONTACT")
    }
}

func didEnd(_ contact: SKPhysicsContact) {
    let bitmaskA = contact.bodyA.categoryBitMask
    let bitmaskB = contact.bodyB.categoryBitMask

    if bitmaskA == bridgeMask || bitmaskB == bridgeMask {
        playerIsOnBridge = false
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM