title: Swift: Run function after collecting item
body: <p>I have a class that creates collectable items:</p>
  
  <pre><code>class CollectableCreate {
  
      var collectable = SKSpriteNode()
      var type = String()
  
      var onCollect : () -&gt; Void = {}
  
      init(type: String, position: CGPoint, text: String = "This collectable uses no text!", onCollect : () -&gt; Void) {
  
          self.type = type.lowercaseString
  
          switch self.type {
  
              default:
                  collectable.size = CGSize(width: 1, height: 200)
                  collectable.color = SKColor.blueColor()
                  collectable.physicsBody = SKPhysicsBody(rectangleOfSize: collectable.size)
                  break
          }
  
          collectable.position = position
  
          collectable.name = self.type
          collectable.physicsBody?.categoryBitMask = PhysicsCategory.Collectable
          collectable.physicsBody?.collisionBitMask = PhysicsCategory.Player
          collectable.physicsBody?.contactTestBitMask = PhysicsCategory.Player
  
  
          self.onCollect = onCollect
      }
  
  
      func chatBoxActivate() -&gt; SKSpriteNode {
          collectable.physicsBody?.dynamic = false
          collectable.physicsBody?.affectedByGravity = false
          return collectable
      }
  
      func collect() {
          self.onCollect()
      }
  
  }
  </code></pre>
  
  <p>I create the collectable item like this:</p>
  
  <pre><code>let d = CollectableCreate(type: "chatbox", position: CGPoint(x: 500, y: 100), onCollect: {
          self.createChatBox("test blalb|blablalba")
      })
  
  self.addChild(d.chatBoxActivate())
  </code></pre>
  
  <p>And on didBeginContact I have this:</p>
  
  <pre><code>if (firstBody.categoryBitMask == PhysicsCategory.Collectable || secondBody.categoryBitMask == PhysicsCategory.Collectable) {
  
      let collectableBody = (firstBody.categoryBitMask == PhysicsCategory.Collectable ? firstBody.node : secondBody.node)
      //collectableBody.collect() &lt;---                
      collectableBody?.removeFromParent()
  
  }
  </code></pre>
  
  <p>I want to run the <code>CollectableCreate().collect()</code> function when I run into a collectable. How to I make this happen?</p>