swiftsprite-kitskspritenodeskphysicsbody

Checking current spritenode


Have that situation: I have bouncing red ball and several randomly generated platforms. In case of contact with bottom of the platform ball should pass platform through. In other case, it will be top of platform and ball should bounce. I changing .collisionBitMask and .contactTestBitMask params for player to pass through platform or bounce on it. But my code working only for the last one platform, you can see on screenshot. For other platforms not working - ball pass it through from the top side too. I have array of that platforms and don't know how to check current platform.

Here is part of my code:

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


               }
            }
        }
    }
}

So, I don't know how to check current platform. If I placing 3 constant platforms by hand all is working fine :D

Please help, don't know how to realize this task.


Solution

  • I have this approach for solutions (Red platforms)

    enter image description here

    You can get code example here: https://github.com/Maetschl/SpriteKitExamples/tree/master/PlatformTest

    This use object velocity for getting the direction and add collision mask

    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
        }
    
    }