title: Basic Swift SpriteKit Collisions using PhysicsBodys
body: <p><strong>The problem:</strong> I seem to be having a little trouble getting my player to collide with a coin, and adding +1 to a coinLabel upon the collision. The player should continue moving after coming in contact with the coin.</p>
  
  <p><strong>What I have now:</strong> With the code I have now, the player travels through the coin, but there is no collision that takes place and +1 isn't added to the coin label. </p>
  
  <p>I am still learning the swift language, so I appreciate any help that is given.</p>
  
  <p>Code: </p>
  
  <pre><code>struct ColliderType {
  
  static let playerCategory: UInt32 = 0x1 &lt;&lt; 0
  
  static let boundary: UInt32 = 0x1 &lt;&lt; 1
  ​  
  ​static let coinCategory: UInt32 = 0x1 &lt;&lt; 2
  ​
  ​static let bodyA: UInt32 = 0x1 &lt;&lt; 4
  ​
  ​static let bodyB: UInt32 = 0x1 &lt;&lt; 8
  
  }
  
  ​override func didMoveToView(view: SKView) {
  ​
  var coinInt = 0
  ​
  ​
  ​
  ​self.physicsWorld.gravity = CGVectorMake(0.0, -7.0)
  physicsWorld.contactDelegate = self
  
  player = SKSpriteNode(imageNamed: "player")
  player.zPosition = 1
  player.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
  player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 5.12)
  player.physicsBody?.dynamic = true
  player.physicsBody?.allowsRotation = false
  
  self.addChild(player)
  
  generateCoins()
  
  ​
  ​
  coin = SKSpriteNode( imageNamed: "coin")
  coin.physicsBody? = SKPhysicsBody(circleOfRadius: coin.size.height / 10)
  coin.physicsBody?.dynamic = false
  coin.physicsBody?.allowsRotation = false
  coin.zPosition = 1
  ​self.addChild(coin)
  ​
  player.physicsBody?.categoryBitMask = ColliderType.playerCategory
  ​player.physicsBody?.contactTestBitMask = ColliderType.boundary
  player.physicsBody?.collisionBitMask = ColliderType.coinCategory | ColliderType.boundary
  
  coin.physicsBody?.categoryBitMask = ColliderType.coinCategory
  coin.physicsBody?.contactTestBitMask = ColliderType.playerCategory
  coin.physicsBody?.collisionBitMask = ColliderType.playerCategory
  
  func didPlayerCollideWithCoin(player: SKSpriteNode, coin: SKSpriteNode) {
  
  
  
  self.coin.removeFromParent()
  
  self.coin += 1
  
  coinLabel.text = "\(coinInt)"
  
  }
  
  ​
  ​    
  ​
  ​func generateCoins()  {
  
  if(self.actionForKey("spawning") != nil){return}
  
  let coinTimer = SKAction.waitForDuration(7, withRange: 2)
  
  let spawnCoin = SKAction.runBlock {
  
  self.coin = SKSpriteNode( imageNamed: "coin")
  
  self.coin.physicsBody = SKPhysicsBody(circleOfRadius: self.coin.size.height / 10)
  
  self.coin.name = "coin"
  
  self.coin.physicsBody?.dynamic = false
  
  self.coin.physicsBody?.allowsRotation = false
  
  var coinPosition = Array&lt;CGPoint&gt;()
  
  coinPosition.append((CGPoint(x:340, y:103)))
  
  coinPosition.append((CGPoint(x:340, y:148)))
  
  coinPosition.append((CGPoint(x:340, y:218)))
  
  coinPosition.append((CGPoint(x: 340, y:343)))
  
  let spawnLocation =     coinPosition[Int(arc4random_uniform(UInt32(coinPosition.count)))]
  
  let action = SKAction.repeatActionForever(SKAction.moveToX(+self.xScale, duration: 4.4))
  
  self.coin.runAction(action)
  
  self.coin.position = spawnLocation
  
  self.addChild(self.coin)
  
  print(spawnLocation)
  
  }
  
  let sequence = SKAction.sequence([coinTimer, spawnCoin])
  
  self.runAction(SKAction.repeatActionForever(sequence), withKey: "spawning")
  
  }
  ​​
  func didBeginContact(contact:SKPhysicsContact)  {
  
  let bodyA: SKPhysicsBody = contact.bodyA
  
  let bodyB: SKPhysicsBody = contact.bodyB
  
  if ((bodyA.categoryBitMask == ColliderType.playerCategory) &amp;&amp;     (bodyB.categoryBitMask == ColliderType.coinCategory)){
  
  didPlayerCollideWithCoin(bodyA.node as! SKSpriteNode, coin: bodyB.node as! SKSpriteNode)
  
  }
      ​        
  ​}
  </code></pre>