0

I Cannot seem to get the 2 sprites to make contact with one another, as when the app runs one of the sprites does not make contact but just goes pass it. I'm not sure whats wrong. Could someone please help me.

import SpriteKit
import GameplayKit

enum BodyType:UInt32{

    case caveMan = 1
    case Zombie = 2
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    var caveMan:SKSpriteNode = SKSpriteNode ()
    let swipeRightRec = UISwipeGestureRecognizer ()
    var Zombie:SKSpriteNode = SKSpriteNode ()

    override func didMove(to view: SKView) {

        self.physicsWorld.contactDelegate = self

        swipeRightRec.addTarget(self, action: #selector (GameScene.swipedRight ))
        swipeRightRec.direction = .right
        self.view!.addGestureRecognizer(swipeRightRec)

        if let somePlayer:SKSpriteNode = self.childNode(withName: "caveMan") as? SKSpriteNode {
            caveMan = somePlayer
            caveMan.physicsBody?.affectedByGravity = true
            caveMan.physicsBody?.isDynamic = false
            caveMan.physicsBody?.categoryBitMask = BodyType.caveMan.rawValue
            caveMan.physicsBody?.collisionBitMask = BodyType.Zombie.rawValue
            caveMan.physicsBody?.contactTestBitMask = BodyType.Zombie.rawValue
        }

        if let somePlayer:SKSpriteNode = self.childNode(withName: "Zombie") as? SKSpriteNode {
            Zombie = somePlayer
            Zombie.physicsBody?.affectedByGravity = false
            Zombie.physicsBody?.isDynamic = false
            Zombie.physicsBody?.categoryBitMask = BodyType.Zombie.rawValue
            Zombie.physicsBody?.collisionBitMask = BodyType.caveMan.rawValue
            Zombie.physicsBody?.contactTestBitMask = BodyType.caveMan.rawValue   
        }
    }

    @objc func swipedRight() {

        print("went right")   
        moveDown()   
    }

    func moveDown() {

        let walkAnimation:SKAction = SKAction(named: "Running")!
        let walk:SKAction = SKAction.moveBy(x: 90, y: 0, duration: 1)
        let group:SKAction = SKAction.group([walkAnimation, walk])

        caveMan.run(group)  
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for t in touches {
            self.touchDown(atPoint: t.location(in: self))
            break
        }
    }

    func didBegin(_ contact: SKPhysicsContact) {

        if (contact.bodyA.categoryBitMask == BodyType.caveMan.rawValue && contact.bodyB.categoryBitMask == BodyType.Zombie.rawValue) {

            print ("touched a Zombie")
        } else if (contact.bodyB.categoryBitMask == BodyType.caveMan.rawValue && contact.bodyA.categoryBitMask == BodyType.Zombie.rawValue) {

            print ("touched a Zombie")
        }
    }
}
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • You REALLY need to clean up your code before posting it in a question, also please don't post funcs that are commented out or have no relevance to your question – Ron Myschuk Nov 20 '17 at 18:11

1 Answers1

0

See this: caveMan.physicsBody? That means if a body exists then allow a value to be set. It is called optional binding, or short circuiting in other languages.

You need to create the physics body, it is not given to you for free.

As of right now this is what your code looks like:

    caveMan = somePlayer
    nil.affectedByGravity = true
    nil.isDynamic = false
    nil.categoryBitMask = BodyType.caveMan.rawValue
    nil.collisionBitMask = BodyType.Zombie.rawValue
    nil.contactTestBitMask = BodyType.Zombie.rawValue

what you want to do is:

    caveMan = somePlayer
    caveMan.physicsBody = SKPhysicsBody(rectangleOf: caveMan.size)
    caveMan.physicsBody!.affectedByGravity = true
    caveMan.physicsBody!.isDynamic = false
    caveMan.physicsBody!.categoryBitMask = BodyType.caveMan.rawValue
    caveMan.physicsBody!.collisionBitMask = BodyType.Zombie.rawValue
    caveMan.physicsBody!.contactTestBitMask = BodyType.Zombie.rawValue
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44