0

Hi i need my player (sprite) to interact with my ground. Without the didbegincontact function override and result in game over, i only want game over when player collides with posts. Please find the code below.

class GameScene: SKScene, SKPhysicsContactDelegate {

override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self



let playerGroup: UInt32 = 1
let PostGroup: UInt32 = 2
let groundGroup: UInt32 = 3
var gameOver = "1"

    //player
    player.physicsBody?.categoryBitMask = playerGroup
    player.physicsBody?.collisionBitMask = PostGroup
    player.physicsBody?.contactTestBitMask = postGroup

    //ground.
    ground.physicsBody?.categoryBitMask = groundGroup

    //post.
    post.physicsBody?.categoryBitMask = PostGroup
    post.physicsBody?.contactTestBitMask = playerGroup


func didBeginContact(contact: SKPhysicsContact!) {

    println("contact")
    println("gameOver")
    gameOver = "1"

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        if (gameOver == "0") {
        player.physicsBody?.velocity = CGVectorMake (0, 0)
        player.physicsBody?.applyImpulse(CGVectorMake(0, 25))
Alex
  • 37
  • 4

1 Answers1

0

Inside of the didBeginContact you need to handle the collision (which are what bit masks are used for)

SKPhysicsContact has two properties: bodyA and bodyB, which are the two bodies used in the collision. Each of these bodies has the bit mask that you set earlier as a property. You can then use the bit mask to tell if the bodyA/bodyB was ground or a post, and then you can handle them differently.

There's a detailed explanation here if you need it

Community
  • 1
  • 1
MaxKargin
  • 1,560
  • 11
  • 13