I'm building a function in which a random variable will choose 1 in 4 SKSpritenode in an array and assign itself to it. However, that randomline, although appear as expected on the screen, does not contain any physicsbody property so it cannot be collide with other node. Below is my code:
func line() {
redLine = SKSpriteNode(imageNamed: "redline")
blueLine = SKSpriteNode(imageNamed: "blueline")
yellowLine = SKSpriteNode(imageNamed: "yellowline")
greenLine = SKSpriteNode(imageNamed: "greenline")
let lineArray = [redLine,blueLine,yellowLine,greenLine]
// Add physics
lineArray[0].physicsBody?.categoryBitMask = gamePhysics.RedLine
lineArray[1].physicsBody?.categoryBitMask = gamePhysics.BlueLine
lineArray[2].physicsBody?.categoryBitMask = gamePhysics.YellowLine
lineArray[3].physicsBody?.categoryBitMask = gamePhysics.GreenLine
for i in 0...3 {
lineArray[i].physicsBody = SKPhysicsBody(circleOfRadius: 10)
lineArray[i].physicsBody?.collisionBitMask = gamePhysics.RedBall | gamePhysics.BlueBall | gamePhysics.YellowBall | gamePhysics.GreenBall
lineArray[i].physicsBody?.contactTestBitMask = gamePhysics.RedBall | gamePhysics.BlueBall | gamePhysics.YellowBall | gamePhysics.GreenBall
lineArray[i].physicsBody?.affectedByGravity = false
lineArray[i].physicsBody?.isDynamic = true
}
let randomLine:SKSpriteNode! = lineArray.randomElement()
...
self.addChild(randomLine)
...
randomLine.run(SKAction.sequence([moveLine,delay,removeLine]))
}
So basically when the randomline collide with other node, nothing happen.
Here is my didBegin (contact) func:
func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == gamePhysics.RedBall && secondBody.categoryBitMask == gamePhysics.RedLine || firstBody.categoryBitMask == gamePhysics.BlueBall && secondBody.categoryBitMask == gamePhysics.BlueLine || firstBody.categoryBitMask == gamePhysics.YellowBall && secondBody.categoryBitMask == gamePhysics.YellowLine || firstBody.categoryBitMask == gamePhysics.GreenBall && secondBody.categoryBitMask == gamePhysics.GreenLine {
currentScore += 1
secondBody.node?.removeFromParent()
print("hit point")
} else if firstBody.categoryBitMask == gamePhysics.RedLine && secondBody.categoryBitMask == gamePhysics.RedBall || firstBody.categoryBitMask == gamePhysics.BlueLine && secondBody.categoryBitMask == gamePhysics.BlueBall || firstBody.categoryBitMask == gamePhysics.YellowLine && secondBody.categoryBitMask == gamePhysics.YellowBall || firstBody.categoryBitMask == gamePhysics.GreenLine && secondBody.categoryBitMask == gamePhysics.GreenBall {
currentScore += 1
firstBody.node?.removeFromParent()
print("hit point")
}
}
Any help would be appreciated!
EDIT: Here is the ball func(). Maybe somehow the SKNode contain multiple categorybitmask and therefore, won't work?
func Ball() {
ballNode = SKNode()
redBall = SKSpriteNode(imageNamed: "redball")
blueBall = SKSpriteNode(imageNamed: "blueball")
yellowBall = SKSpriteNode(imageNamed: "yellowball")
greenBall = SKSpriteNode(imageNamed: "greenball")
let ballArray = [redBall,blueBall,yellowBall,greenBall]
...
// Add physics
ballArray[0].physicsBody?.categoryBitMask = gamePhysics.RedBall
ballArray[1].physicsBody?.categoryBitMask = gamePhysics.BlueBall
ballArray[2].physicsBody?.categoryBitMask = gamePhysics.YellowBall
ballArray[3].physicsBody?.categoryBitMask = gamePhysics.GreenBall
for i in 0...3 {
ballArray[i].physicsBody = SKPhysicsBody(circleOfRadius: 20)
ballArray[i].physicsBody?.collisionBitMask = gamePhysics.RedLine | gamePhysics.BlueLine | gamePhysics.YellowLine | gamePhysics.GreenLine
ballArray[i].physicsBody?.contactTestBitMask = gamePhysics.RedLine | gamePhysics.BlueLine | gamePhysics.YellowLine | gamePhysics.GreenLine
ballArray[i].physicsBody?.affectedByGravity = false
ballArray[i].physicsBody?.isDynamic = false
}
ballNode.addChild(redBall)
ballNode.addChild(greenBall)
ballNode.addChild(yellowBall)
ballNode.addChild(blueBall)
self.addChild(ballNode)
}
UPDATE 2:
Here is a struct where i store my categorybitmask:
struct gamePhysics {
static let RedBall : UInt32 = 0x1 << 1
static let BlueBall : UInt32 = 0x1 << 2
static let GreenBall : UInt32 = 0x1 << 3
static let YellowBall : UInt32 = 0x1 << 4
static let RedLine : UInt32 = 0x1 << 5
static let BlueLine : UInt32 = 0x1 << 6
static let GreenLine : UInt32 = 0x1 << 7
static let YellowLine : UInt32 = 0x1 << 8
}
Did you remember setting the contact delegate inside your scene? Place this line of code inside didMove(to:):
self.physicsWorld.contactDelegate = self
If this don't solve the issue, I would make the if conditions inside didBegin more explicit using parentheses between the && and || statements.
Looking to the rest of the code I can not see what else could be wrong, what you did should work. Are the print("hit point") you placed inside the if statements been reached?
OMG i found the answer to my question. The reason for the value of the categorybitmask to return "nil" was because i actually assign the node to categorybitmask before specified its physicsbody. Move the line down a few line and everything is solved!
Related
I try to make objects moving down the screen with 2 categories. On the bottom I have 2 cars (left and right), and if the objects hit the car 1 is a "collision" and the other is a "spinner" that will spinn the car around....
Object setup have a random of "collision" and "spinner" object....
Only "spinner" objects should be detected now as I not added "collision" yet.
My problem is that all are treated the same, so spinner and collision objects get "Hit right car! / Hit left car!" in my console.
"Hit left tree! / "Hit right tree!" never triggers.
Code:
func didBegin(_ contact: SKPhysicsContact) {
if enableContact == true {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask & PC.L_CAR) != 0 && (secondBody.categoryBitMask & PC.L_COLLIDER) != 0 {
print ("Hit left car!")
}
else if (firstBody.categoryBitMask & PC.R_CAR) != 0 && (secondBody.categoryBitMask & PC.R_COLLIDER) != 0 {
print ("Hit right car!")
}
else if (firstBody.categoryBitMask & PC.L_CAR) != 0 && (secondBody.categoryBitMask & PC.L_SPINNER) != 0 {
print("Hit left tree!")
}
else if (firstBody.categoryBitMask & PC.R_CAR) != 0 && (secondBody.categoryBitMask & PC.R_SPINNER) != 0 {
print("Hit right tree!")
}
}}
Car setup:
func setUp() {
canMove = true
leftCar = self.childNode(withName: "leftCar") as! SKSpriteNode
rightCar = self.childNode(withName: "rightCar") as! SKSpriteNode
centerPoint = self.frame.size.width / self.frame.size.height
leftCar.physicsBody?.isDynamic = true
leftCar.physicsBody?.categoryBitMask = PC.L_CAR
leftCar.physicsBody?.contactTestBitMask = PC.L_SPINNER
leftCar.physicsBody?.collisionBitMask = 0
rightCar.physicsBody?.isDynamic = true
rightCar.physicsBody?.categoryBitMask = PC.R_CAR
rightCar.physicsBody?.contactTestBitMask = PC.R_SPINNER
rightCar.physicsBody?.collisionBitMask = 0
Object setup:
#objc func leftTraffic() {
if !stopEverything {
let leftTrafficItem : SKSpriteNode!
let randonNumber = Helper().randomBetweenTwoNumbers(firstNumber: 1, secondNumber: 13)
switch Int(randonNumber) {
case 1...4:
leftTrafficItem = SKSpriteNode(imageNamed: "orangeCar")
leftTrafficItem.name = "orangeCar"
leftTrafficItem.physicsBody?.categoryBitMask = PC.L_COLLIDER
break
case 5...8:
leftTrafficItem = SKSpriteNode(imageNamed: "greenCar")
leftTrafficItem.name = "greenCar"
leftTrafficItem.physicsBody?.categoryBitMask = PC.L_COLLIDER
break
case 9...11:
leftTrafficItem = SKSpriteNode(imageNamed: "rock")
leftTrafficItem.name = "rock"
leftTrafficItem.physicsBody?.categoryBitMask = PC.L_COLLIDER
break
case 12...13:
leftTrafficItem = SKSpriteNode(imageNamed: "tree")
leftTrafficItem.name = "tree"
leftTrafficItem.physicsBody?.categoryBitMask = PC.L_SPINNER
break
default:
leftTrafficItem = SKSpriteNode(imageNamed: "orangCar")
leftTrafficItem.name = "orangeCar"
leftTrafficItem.physicsBody?.categoryBitMask = PC.L_COLLIDER
}
leftTrafficItem.anchorPoint = CGPoint(x: 0.5, y: 0.5)
leftTrafficItem.zPosition = 10
let randomNum = Helper().randomBetweenTwoNumbers(firstNumber: 1, secondNumber: 10)
switch Int(randomNum) {
case 1...4:
leftTrafficItem.position.x = -280
break
case 5...10:
leftTrafficItem.position.x = -100
break
default:
leftTrafficItem.position.x = -280
}
leftTrafficItem.position.y = 700
leftTrafficItem.physicsBody = SKPhysicsBody(texture: leftTrafficItem.texture!, size: (leftTrafficItem.texture?.size())!)
leftTrafficItem.physicsBody?.isDynamic = true
leftTrafficItem.physicsBody?.contactTestBitMask = 0
leftTrafficItem.physicsBody?.collisionBitMask = 0
leftTrafficItem.physicsBody?.affectedByGravity = false
addChild(leftTrafficItem)
}
}
I created a function that continuously spawns coins forever.
However, I'm having trouble removing the coin that is touched by the ball that hits them. By the way, I have a variable called coinBoolean set to false in the class. Here is my code:
func didBeginContact(contact: SKPhysicsContact) {
if ((contact.bodyA.categoryBitMask == groundGroup && contact.bodyB.categoryBitMask == ballGroup) ||
(contact.bodyA.categoryBitMask == ballGroup && contact.bodyB.categoryBitMask == groundGroup)) {
}
if ((contact.bodyA.categoryBitMask == ballGroup && contact.bodyB.categoryBitMask == coinGroup) ||
(contact.bodyA.categoryBitMask == coinGroup && contact.bodyB.categoryBitMask == ballGroup)) {
print("TRUE")
coinBoolean = true
}
else {
print("false")
coinBoolean = false
}
}
func addAndMoveCoins() {
let coin = SKSpriteNode(imageNamed: "Coin")
coin.zPosition = 55
coin.position = CGPoint(x: self.frame.size.width*0.9, y: randomY())
coin.physicsBody = SKPhysicsBody(rectangleOfSize: coin.size)
coin.physicsBody?.categoryBitMask = coinGroup
coin.physicsBody?.contactTestBitMask = ballGroup
coin.physicsBody?.affectedByGravity = false
coin.physicsBody?.dynamic = false
let moveCoinLeft = SKAction.moveByX(-self.size.height, y: 0, duration: 5)
let repeatAction1 = SKAction.repeatActionForever(moveCoinLeft)
let removeObstacle1 = SKAction.removeFromParent()
let moveAndRemove1 = SKAction.sequence([repeatAction1, removeObstacle1])
coin.runAction(moveAndRemove1)
self.addChild(coin)
if coinBoolean == true {
coin.removeFromParent()
}
else if coinBoolean == false {
}
}
func repeatCoins() {
let generateCoins = SKAction.sequence([SKAction.runBlock(self.addAndMoveCoins), SKAction.waitForDuration(1.3)])
let endlessAction = SKAction.repeatActionForever(generateCoins)
runAction(endlessAction)
}
Your coinBoolean variable is probably changing too fast. I suggest getting the node you want to remove from the collision (bodyA or bodyB) and removing it from its parent
Just set an action on the coin object during the contact phase instead of doing booleans.
contact.bodyA.node.runAction(SKAction.removeFromParent())
You would need to check if bodyA is the coin object of course
I'm trying to make a simple Space Shooter game. The contact should happen either between the torpedo and the alien or the shuttle and the alien. The problem is that this second contact (shuttle vs. alien) only happens after the first kind of contact has happend (torpedo vs. alien) and further more they're not always precise. This is a struct created outside the class
struct PhysicsCategory {
static let alien : UInt32 = 1
static let torpedo : UInt32 = 2
static let shuttle : UInt32 = 3 }
Shuttle:
shuttle.physicsBody = SKPhysicsBody(rectangleOfSize: shuttle.size)
shuttle.physicsBody?.categoryBitMask = PhysicsCategory.shuttle
shuttle.physicsBody?.contactTestBitMask = PhysicsCategory.alien
shuttle.physicsBody?.dynamic = false
shuttle.physicsBody?.affectedByGravity = false
Torpedo:
torpedo.physicsBody = SKPhysicsBody(rectangleOfSize: torpedo.size)
torpedo.physicsBody?.categoryBitMask = PhysicsCategory.torpedo
torpedo.physicsBody?.contactTestBitMask = PhysicsCategory.alien
torpedo.physicsBody?.affectedByGravity = false
torpedo.physicsBody?.dynamic = false
Alien:
alien.physicsBody = SKPhysicsBody(rectangleOfSize: torpedo.size)
alien.physicsBody?.categoryBitMask = PhysicsCategory.alien
alien.physicsBody?.contactTestBitMask = PhysicsCategory.torpedo
alien.physicsBody?.affectedByGravity = false
alien.physicsBody?.dynamic = true
Finally, here's my contact code:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCategory.alien) && (secondBody.categoryBitMask == PhysicsCategory.torpedo)) ||
((firstBody.categoryBitMask == PhysicsCategory.torpedo) && (secondBody.categoryBitMask == PhysicsCategory.alien)) {
self.contactWithTorpedo(firstBody.node as! SKSpriteNode, torpedo: secondBody.node as! SKSpriteNode)
} else if ((firstBody.categoryBitMask == PhysicsCategory.shuttle) && (secondBody.categoryBitMask == PhysicsCategory.alien)) {
self.contactWithShuttle(firstBody.node as! SKSpriteNode, shuttle: secondBody.node as! SKSpriteNode)
}
}
func contactWithTorpedo (alien: SKSpriteNode, torpedo : SKSpriteNode) {
alien.removeFromParent()
torpedo.removeFromParent()
score++
scoreLabel.text = "score: " + "\(score)"
}
func contactWithShuttle (alien:SKSpriteNode, shuttle:SKSpriteNode) {
alien.removeFromParent()
shuttle.removeFromParent()
self.view?.presentScene(EndScene())
}
I'm not really sure where the problem is, plus I've seen a couple of tutorials do the same. I don't know if it's relevant by the way, but this is not an iOS game but an OSX. Thank you in advance!
You might find it less confusing to restructure your didBeginContact as follows, as this avoids the firstBody/secondbody stuff and the complicated if...then conditions to see what has contacted what:
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case PhysicsCategory.alien | PhysicsCategory.torpedo:
// alien and torpedo have contacted
contact.bodyA.removeFromParent()
contact.bodyB.removeFromParent()
score += 1
scoreLabel.text = "score: " + "\(score)"
case PhysicsCategory.alien | PhysicsCategory.shuttle:
// alien and shuttle have contacted
contact.bodyA.removeFromParent()
contact.bodyB.removeFromParent()
self.view?.presentScene(EndScene())
default :
//Some other contact has occurred
print("Some other contact")
}
}
You can just add as many PhysicsCategory.enemy | PhysicsCategory.player cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else.
if you do need to reference only one of the nodes involved in a contact, (e.g. to remove the player after an enemy hits it), you can do it like this:
let playerNode = contact.bodyA.categoryBitMask == PhysicsCategory.player ? contact.bodyA.node! : contact.bodyB.node!
playernode.removefromParent
I would recommend you to read the the docs about SKPhysicsBody.
Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions
First of all I would change the PhysicsCategory to
struct PhysicsCategory {
static let alien : UInt32 = 0x1 << 1
static let torpedo : UInt32 = 0x1 << 2
static let shuttle : UInt32 = 0x1 << 3
}
Then
alien.physicsBody?.contactTestBitMask = PhysicsCategory.torpedo | PhysicsCategory.shuttle
Hope this helps.
So I've actually managed to solve my problem yesterday. I'm posting the updated code in case it could help someone.
Outside the class:
struct PhysicsCategory {
static let player : UInt32 = 0x1 << 0
static let bullet : UInt32 = 0x1 << 1
static let enemy : UInt32 = 0x1 << 2}
And then, after applying the phyics to each sprite as i wrote before, inside the class:
//Contact with bullet
func contactWithBullet(enemy : SKSpriteNode, bullet: SKSpriteNode) {
enemy.removeFromParent()
bullet.removeFromParent()
score += 1
updateLabels()
}
//contact with player
func contactWithPlayer(player : SKSpriteNode, enemy : SKSpriteNode) {
enemy.removeFromParent()
lives -= 1
updateLabels() //another function that changes the score and lives labels
}
//CONTACT DETECTION
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if (firstBody.categoryBitMask == PhysicsCategory.enemy && secondBody.categoryBitMask == PhysicsCategory.bullet || firstBody.categoryBitMask == PhysicsCategory.bullet && secondBody.categoryBitMask == PhysicsCategory.enemy) {
contactWithBullet(firstBody.node as! SKSpriteNode, bullet: secondBody.node as! SKSpriteNode)
checkScore()
enemiesInWave -= 1
} else if (firstBody.categoryBitMask == PhysicsCategory.enemy && secondBody.categoryBitMask == PhysicsCategory.player || firstBody.categoryBitMask == PhysicsCategory.player && secondBody.categoryBitMask == PhysicsCategory.enemy) {
contactWithPlayer(firstBody.node as! SKSpriteNode, enemy: secondBody.node as! SKSpriteNode)
checkLives()
enemiesInWave -= 1
}
}
So, I am still experimenting with Sprite Kit for my first time ever, and I would like to test for collision. So, I searched around a bit in Apple's documentation, around Stack Overflow, online tutorials, and other forums. However, I was unable to find something a tip or code that makes what I am doing work. So, here are the relevant pieces of code:
This is the code for an obstacle:
func createObstacle(){
var ball = SKShapeNode(circleOfRadius: 20)
var width = UInt32(self.frame.width)
var random_number = arc4random_uniform(width)
ball.position = CGPointMake(CGFloat(random_number), frame.height+20)
ball.strokeColor = SKColor.blackColor()
ball.glowWidth = 1.0
ball.fillColor = SKColor.darkGrayColor()
ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
ball.physicsBody!.affectedByGravity = true
ball.physicsBody?.categoryBitMask = 6
ball.physicsBody?.dynamic = true
self.addChild(ball)
}
This is relevant code for the thing that it would collide with:
let circle = SKShapeNode(circleOfRadius: 20)
circle.physicsBody = SKPhysicsBody(circleOfRadius: 20)
circle.fillColor = SKColor.blueColor()
circle.strokeColor = SKColor.blueColor()
circle.glowWidth = 1.0
circle.physicsBody?.categoryBitMask = 4
circle.physicsBody?.dynamic = true
circle.physicsBody?.affectedByGravity = false
And this is the code for contact:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody!
var secondBody: SKPhysicsBody!
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask == 4 && secondBody.categoryBitMask == 6) || (firstBody.categoryBitMask == 6 && secondBody.categoryBitMask == 4)){
println("HI")
}else{println("NO")}
}
Sadly, nothing is being printed at all, so something's wrong. Any idea why this doesn't work?
Your class should have delegate SKPhysicsContactDelegate.
class GameScene: SKScene, SKPhysicsContactDelegate {
In didMoveToView write this:
physicsWorld.contactDelegate = self
EDIT
Define CategoryBitMask like this
struct PhysicsCategory {
static let circleCategory : UInt32 = 0b1 // 1
static let ballCategory : UInt32 = 0b10 // 2
}
Give CategoryBitMask to circle and ball
circle.physicsBody?.categoryBitMask = PhysicsCategory.circleCategory
ball.physicsBody?.categoryBitMask = PhysicsCategory.ballCategory
Then check contact like this:
(func didBeginContact(contact: SKPhysicsContact) {
if ((contact.bodyA.categoryBitMask == 0b1 && contact.bodyB.categoryBitMask == 0b10 ) || ( contact.bodyA.categoryBitMask == 0b1 && contact.BodyB.categoryBitMask == 0b1 ))
println("Contact")
}
}
Sorry for typos didnt used editor
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:
http://pastebin.com/7yGh5gBn