3

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
    }
}
}
at26
  • 63
  • 5

1 Answers1

2

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

Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • thank you fro the response! What would be the command to stop the movement of the ball? I've seen isPaused = true and RemoveAllActions() but neither of them stop the ball. – at26 Mar 29 '17 at 23:00
  • Please check the code I have. I edited it on the original comment. – at26 Mar 29 '17 at 23:02
  • 1
    Assuming you are using physics impulses to move the objects, set the velocity to 0,0 something like SmallBall.physicsBody?.velocity = CGVector(dx: 0, dy: 0) – Ron Myschuk Mar 29 '17 at 23:10