I'm new with Xcode and I'm trying to make a flappy bird game but I'm having troubles with the score and collision code, the game runs but it is infinite because the character never dies when touch the walls or ground and when I take the coins the score level don't increase, someone can help me with this please?
func createScene(){
self.physicsWorld.contactDelegate = self
for i in 0..<2 {
let background = SKSpriteNode(imageNamed: "Background")
background.anchorPoint = CGPoint.zero
background.position = CGPoint(x: CGFloat(i) * self.frame.width,y: 0)
background.name = "background"
background.size = (self.view?.bounds.size)!
self.addChild(background)
}
scoreLbl.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + self.frame.height / 2.5)
scoreLbl.text = "\(score)"
scoreLbl.zPosition = 6
scoreLbl.fontSize = 60
self.addChild(scoreLbl)
Ground = SKSpriteNode(imageNamed: "Ground")
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: 0 + Ground.frame.height / 2)
Ground.physicsBody = SKPhysicsBody(rectangleOf: Ground.size)
Ground.physicsBody?.categoryBitMask = PhysicsCatagory.Ground
Ground.physicsBody?.collisionBitMask = PhysicsCatagory.Ghost
Ground.physicsBody?.contactTestBitMask = PhysicsCatagory.Ghost
Ground.physicsBody?.affectedByGravity = false
Ground.physicsBody?.isDynamic = false
Ground.zPosition = 3
self.addChild(Ground)
Ghost = SKSpriteNode(imageNamed: "Ghost")
Ghost.size = CGSize(width: 60, height: 70)
Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 2)
Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
Ghost.physicsBody?.affectedByGravity = false
Ghost.physicsBody?.isDynamic = true
Ghost.zPosition = 2
self.addChild(Ghost)
}
override func didMove(to view: SKView) {
/* Setup your scene here */
createScene()
}
func createBTN(){
restartBTN = SKSpriteNode(imageNamed: "RestartBtn")
restartBTN.size = CGSize(width: 200,height: 100)
restartBTN.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
restartBTN.zPosition = 6
restartBTN.setScale(0)
self.addChild(restartBTN)
restartBTN.run(SKAction.scale(to: 1.0, duration: 0.3))
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == PhysicsCatagory.Score && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
score+=1
scoreLbl.text = "score"
firstBody.node?.removeFromParent()
}
else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.Score {
score+=1
scoreLbl.text = "score"
secondBody.node?.removeFromParent()
}
else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.Wall || firstBody.categoryBitMask == PhysicsCatagory.Wall && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
enumerateChildNodes(withName: "wallPair", using: ({
(node, error) in
node.speed = 0
self.removeAllActions()
}))
if died == false{
died = true
createBTN()
}
}
else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.Ground || firstBody.categoryBitMask == PhysicsCatagory.Wall && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
enumerateChildNodes(withName: "wallPair", using: ({
(node, error) in
node.speed = 0
self.removeAllActions()
}))
if died == false{
died = true
createBTN()
}
}
}