检查当前精灵节点

Checking current spritenode

有这种情况:我有弹跳的红球和几个随机生成的平台。 在接触平台底部的情况下,球应穿过平台。在其他情况下,它将位于平台顶部并且球应该反弹。我更改 .collisionBitMask 和 .contactTestBitMask 参数让玩家通过平台或在平台上弹跳。但是我的代码只适用于最后一个平台,你可以在 screenshot 上看到。对于其他不工作的平台 - 球也从顶部穿过。我有一系列的平台,但不知道如何检查当前平台。

这是我的部分代码:

var platforms = [SKSpriteNode]()

.....

extension GameScene: SKPhysicsContactDelegate {

func didBegin(_ contact: SKPhysicsContact) {
    let floor = BitMaskCategory.floor
    let player = BitMaskCategory.player
    let platformCategory = BitMaskCategory.platform
    let bodyA = contact.bodyA.categoryBitMask
    let bodyB = contact.bodyB.categoryBitMask


    if bodyA == player && bodyB == platformCategory  || bodyA == platformCategory && bodyB == player {
        for platform in platforms {
            if self.player.position.y > platform.position.y {
                self.player.physicsBody?.collisionBitMask = floor | platformCategory
                self.player.physicsBody?.contactTestBitMask = floor | platformCategory
            } else if self.player.position.y < platform.position.y  {
                self.player.physicsBody?.collisionBitMask = floor
                self.player.physicsBody?.contactTestBitMask = floor
            }
        }
    }
}

func didEnd(_ contact: SKPhysicsContact) {
    let player = BitMaskCategory.player
    let bar = BitMaskCategory.platform
    let floor = BitMaskCategory.floor
    let bodyA = contact.bodyA.categoryBitMask
    let bodyB = contact.bodyB.categoryBitMask

    if bodyA == bar && bodyB == player || bodyA == player && bodyB == bar {
        for platform in platforms {
            if self.player.position.y > platform.position.y  {
                self.player.physicsBody?.collisionBitMask = floor
                self.player.physicsBody?.contactTestBitMask = floor


               }
            }
        }
    }
}

所以,我不知道如何查看当前平台。如果我手动放置 3 个常量平台,一切正常:D

请帮忙,不知道如何实现这个任务。

我有这种解决方案(红色平台)

您可以在此处获取代码示例:https://github.com/Maetschl/SpriteKitExamples/tree/master/PlatformTest

这使用对象速度获取方向和添加 碰撞掩码

override func update(_ currentTime: TimeInterval) {
    player.physicsBody?.collisionBitMask = 0b0000 // Reset the mask

    // For UP only Platform
    if (player.physicsBody?.velocity.dy)! < CGFloat(0.0) {
        player.physicsBody?.collisionBitMask |= 0b0001 // The pipe | operator adds the mask by binary operations
    }

    // For Down only platforms
    if (player.physicsBody?.velocity.dy)! > CGFloat(0.0) {
        player.physicsBody?.collisionBitMask |= 0b0010  // The pipe | operator adds the mask by binary operations
    }

}