I am a very new programmer currently using Swift to make a simple brick breaker like game. I am trying to create a label that dynamically shows the score depending on how many times the ball collides with the paddle. Here are two different parts of my code I have so far.
bottom.physicsBody!.categoryBitMask = BottomCategory
ball.physicsBody!.categoryBitMask = BallCategory
paddle.physicsBody!.categoryBitMask = PaddleCategory
ball.physicsBody!.contactTestBitMask = BottomCategory
I know this may not be much help but I am wondering what type of bitmask I will have to make. Here is a part of my code where I want it to create the label
func didBeginContact(contact: SKPhysicsContact) {
// Make variables for the two physics bodies
var score: Int
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
let label = SKLabelNode(fontNamed: "Chalkduster")
label.text = String(score)
label.fontSize = 40
label.fontColor = SKColor.whiteColor()
label.position = CGPoint (x: 1136, y: 600)
addChild(label)
}
Any help would be appreciated.
To detect collisions you have to set collisionBitMask
and to test contact you have to set the contactTestBitMask
. The Ball has to detect collision with the paddle and detect contact with the bottom and the paddle. So you have to set the contactTestBitMask
as
ball.physicsBody?.contactTestBitMask = BottomCategory | PaddleCategory
And the collisionBitMask
as
ball.physicsBody?.collissionBitMask = PaddleCategory
So your code should be
bottom.physicsBody?.categoryBitMask = BottomCategory
bottom.physicsBody?.contactTestBitMask = BallCategory
paddle.physicsBody?.categoryBitMask = PaddleCategory
paddle.physicsBody?.contactTestBitMask = BallCategory
paddle.physicsBody?.collissionBitMask = BallCategory
ball.physicsBody?.categoryBitMask = BallCategory
ball.physicsBody?.contactTestBitMask = BottomCategory | PaddleCategory
ball.physicsBody?.collissionBitMask = PaddleCategory