I have created a clone of the app Flappy Bird, and I’ve tried to add coins which appear randomly around the game. My problem is I try to make the coins disappear when the bird collides with them, but it has proven a difficult task. I have a function that spawns in a coin every 3 seconds; at first all the coins had the same categoryBitMask's, but then I changed it so every 3 coins had the the categoryBitMask of 1, 2, 3, recurring.
I have tried many slight variations on the code to get rid of the coins as they collide with the bird, sometimes the coins will disappear, but with other problems, like the wrong coins disappearing or no more coins spawning. Here is all the code I think you need to see for my problem:
class GameScene: SKScene, SKPhysicsContactDelegate {
var bird = SKSpriteNode()
var coin = SKSpriteNode()
let birdGroup: UInt32 = 1
let objectGroup: UInt32 = 2
let gapGroup: UInt32 = 0 << 3
let gapGroup2: UInt32 = 0 << 4
let gapGroup3: UInt32 = 0 << 5
var gameOver = 0
var movingObjects = SKNode()
var coinGroup1 = SKNode()
var coinGroup2 = SKNode()
var coinGroup3 = SKNode()
override func didMoveToView(view: SKView) {
/********| Bird physics body |********/
bird.physicsBody?.categoryBitMask = birdGroup
bird.physicsBody?.collisionBitMask = gapGroup
bird.physicsBody?.collisionBitMask = gapGroup2
bird.physicsBody?.collisionBitMask = gapGroup3
bird.physicsBody?.collisionBitMask = objectGroup
bird.physicsBody?.contactTestBitMask = objectGroup
bird.zPosition = 10
self.addChild(bird)
/********| Bird physics body |********/
/********| Spawning coins |********/
var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("spawnPipes"), userInfo: nil, repeats: true)
}
func spawnPipes(){
var coinTexture = SKTexture(imageNamed: "coin.png")
coin = SKSpriteNode(texture: coinTexture)
coin.size.height = bird.size.height
coin.size.width = bird.size.width
coin.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width * 1.33, y: CGRectGetMidY(self.frame) + movementAmount2)
coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2)
coin.physicsBody?.dynamic = false
coin.runAction(moveAndRemove)
coin.physicsBody?.contactTestBitMask = birdGroup
if(i == 1){
coin.physicsBody?.categoryBitMask = gapGroup
coin.physicsBody?.collisionBitMask = gapGroup
coinGroup1.addChild(coin)
}else if(i == 2){
coin.physicsBody?.categoryBitMask = gapGroup2
coin.physicsBody?.collisionBitMask = gapGroup2
coinGroup2.addChild(coin)
}else if(i == 3){
coin.physicsBody?.categoryBitMask = gapGroup3
coin.physicsBody?.collisionBitMask = gapGroup3
coinGroup3.addChild(coin)
}else{
i = 1
}
}
/********| Spawning coins |********/
/********| Removing coins |********/
func didBeginContact(contact: SKPhysicsContact) {
if(gameOver == 0){
if((contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup) || (contact.bodyA.categoryBitMask == gapGroup2 || contact.bodyB.categoryBitMask == gapGroup2) || (contact.bodyA.categoryBitMask == gapGroup3 || contact.bodyB.categoryBitMask == gapGroup3)){
score++
scoreLabel.text = "\(score)"
if(contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup){
coinGroup1.removeAllChildren()
}else if(contact.bodyA.categoryBitMask == gapGroup2 || contact.bodyB.categoryBitMask == gapGroup2){
coinGroup2.removeAllChildren()
}else if(contact.bodyA.categoryBitMask == gapGroup3 || contact.bodyB.categoryBitMask == gapGroup3){
coinGroup3.removeAllChildren()
}
/********| Removing coins |********/
Here is all of my code if this ^^^ isn't enough: