3
votes

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 the side borders. The ball is initially placed at the bottom border, and when the user touches a spot, the ball goes in that direction and bounces off the borders. However, I want the ball to stop once it touches the bottom border again rather than bouncing off of it. I've made it so that the ball bounces off all the border, but I'd like it to only bounce off the top and the sides. My apologies if this code isn't enough for the proper response; I'm relatively new at 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 code for the collision detection. func didBegin(_ 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
    }
}
}
1

1 Answers

2
votes

I would just create the edgeLoop on a slightly longer rectangle (yellow-green outlined box) than the scene (white Box) frame, so that it hangs down below the scene. Make it have all the same properties as you currently have. Then I would create another physicsBody (red box) just below the scene. Then in the didBegin(_ contact: SKPhysicsContact) method detect a collision with the ball and the red box and stop it

example