Swift SpriteKit Create a border where only three sides are passable

I am trying to create a game where the user plays by shooting balls. I want the balls to bounce off the top border and side borders. The ball is initially placed on the bottom border and when the user touches the spot, the ball goes in that direction and bounces off the boundaries. However, I want the ball to stop as soon as it touches the bottom, instead of bouncing off of it. I made the ball bounce off the border, but I would like it to only bounce off the top and to the sides. We apologize if this code is not enough for a correct answer; I am relatively new on this forum :)

     let border = SKPhysicsBody(edgeLoopFrom: self.frame)
    //create variable representing frame of gamescene

    border.friction = 0
    border.restitution = 1
    self.physicsBody = border

      

This is the collision detection code. func madeBegin (_ contact: SKPhysicsContact) {

    if (contact.bodyA.categoryBitMask == BodyType.ball.rawValue && contact.bodyB.categoryBitMask == BodyType.BottomBorder.rawValue){
        SmallBall.isPaused = true
        }
    else if(contact.bodyB.categoryBitMask == BodyType.ball.rawValue && contact.bodyA.categoryBitMask == BodyType.BottomBorder.rawValue)
    {
        SmallBall.isPaused = true
    }
}
}

      

+3


source to share


1 answer


I would just create an edgeLoop on a slightly longer rectangle (yellow-green highlighted box) than the scene frame (white frame) so that it hovers below the scene. You still have the same properties that you have. Then I will create another physical block (red box) just below the scene. Then, in the method, didBegin(_ contact: SKPhysicsContact)

detect a collision with the ball and the red field and stop it



example

+2


source







All Articles