Barkley
Barkley

Reputation: 6683

Collision not working in swift + sprite kit

import SpriteKit



// fix spawning so close to the middle
// get ball col working
// set width apart/ height they must be apart (if statement)


let BallCategoryName = "ball"
let BarCategoryName = "block"
let BarNodeCategoryName = "blockNode"

let GreenBallCategory  : UInt32 = 0x1 << 0
let RedBallCategory: UInt32 = 0x1 << 1
let GreenBarCategory : UInt32 = 0x1 << 2
let RedBarCategory : UInt32 = 0x1 << 3

class GameScene: SKScene,  SKPhysicsContactDelegate {



    //VARIABLES IMPORTANTE//

    var greenBall = SKSpriteNode(imageNamed: "greenball")
    var redBall = SKSpriteNode(imageNamed: "redball")
    var bar0 = SKSpriteNode(imageNamed: "bar0")
    var bar1 = SKSpriteNode(imageNamed: "bar1")
    var bar2 = SKSpriteNode(imageNamed: "bar2")
    var bar3 = SKSpriteNode(imageNamed: "bar3")
    var bar4 = SKSpriteNode(imageNamed: "bar4")
    var bar5 = SKSpriteNode(imageNamed: "bar5")


    override func didMoveToView(view: SKView) {

        physicsWorld.contactDelegate = self


     // BALL CHARACTERISTICS//


    //Green//

        greenBall.position  = CGPoint(x: frame.size.width*0.25,  y:  (frame.size.height * 0.95)/2)
        greenBall.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        greenBall.physicsBody = SKPhysicsBody(circleOfRadius: greenBall.size.width/2)
        greenBall.physicsBody?.friction = 0
        greenBall.physicsBody?.restitution = 1
        greenBall.physicsBody?.linearDamping = 0
        greenBall.physicsBody?.angularDamping = 0
        greenBall.physicsBody?.affectedByGravity = false
        greenBall.name = BallCategoryName
        addChild(greenBall)

    //Red//

        redBall.anchorPoint = CGPoint(x: 0.5 , y: 0.5)
        redBall.position  = CGPoint(x: frame.size.width*0.75 ,  y:  (frame.size.height * 0.95)/2)
        redBall.physicsBody = SKPhysicsBody(circleOfRadius: greenBall.size.width/2)
        redBall.physicsBody?.friction = 0
        redBall.physicsBody?.restitution = 1
        redBall.physicsBody?.linearDamping = 0
        redBall.physicsBody?.angularDamping = 0
        redBall.physicsBody?.affectedByGravity = false
        redBall.physicsBody?.dynamic = false



        addChild(redBall)



        //////////////////////////////////////////////////////


        // SETTING UP SCENE//

        GameScene(size: self.view!.frame.size) //WHy not work

        let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody = borderBody
        self.physicsBody?.friction = 0




        // BACKGROUND COLOR




        // PHYSICS //

        physicsWorld.gravity = CGVectorMake(0, 0)





        //////////////////////////////////////////////////////



        //  EXTRA BARS
        var midBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))
        var menuBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))
        var invisMenuBar = SKShapeNode(rectOfSize: CGSize(width: frame.size.width, height: 0.00001))
        var botBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))

        midBar.fillColor = SKColor.whiteColor()
        menuBar.fillColor = SKColor.whiteColor()
        invisMenuBar.fillColor = SKColor.purpleColor()
        botBar.fillColor = SKColor.whiteColor()

    ////MID BAR////
      var minus = frame.size.height * 0.049
        midBar.position = CGPoint(x: frame.size.width/2, y: (frame.size.height * 0.95)/2)
          addChild(midBar)



    ////BOT BAR////
           botBar.position = CGPoint(x: frame.size.width/2, y: 0 + (botBar.frame.size.height/2))
        addChild(botBar)


    ////MENU BAR////
            menuBar.position = CGPoint(x: frame.size.width/2, y: frame.size.height - (frame.size.height * 0.05))

         addChild(menuBar)

  //// INVIS MENU BAR ////
        invisMenuBar.position = CGPoint(x: frame.size.width/2, y: frame.size.height - (frame.size.height * 0.047))
        invisMenuBar.physicsBody = SKPhysicsBody(rectangleOfSize: invisMenuBar.frame.size)
        invisMenuBar.physicsBody?.friction = 0
        invisMenuBar.physicsBody?.restitution = 1
        invisMenuBar.physicsBody?.linearDamping = 0
        invisMenuBar.physicsBody?.angularDamping = 0
        invisMenuBar.physicsBody?.affectedByGravity = false
        invisMenuBar.physicsBody?.dynamic = false


        invisMenuBar.zPosition = 100
        menuBar.zPosition = 5

        addChild(invisMenuBar)




   // TEST STUFF WITH BARS //

        let chosenOne = SKSpriteNode(imageNamed: "bar4")
        chosenOne.position = CGPoint(x: frame.midX - chosenOne.frame.width/2 - 50 ,  y: self.frame.height * 0.75)





        addChild(chosenOne)


        //////////////////////////////////////////////////////


        //// SETTING UP MY CONTACT ///

        greenBall.physicsBody?.categoryBitMask = GreenBallCategory
        chosenOne.physicsBody?.categoryBitMask = RedBarCategory

        greenBall.physicsBody?.contactTestBitMask = RedBarCategory
        chosenOne.physicsBody?.contactTestBitMask = GreenBallCategory

        chosenOne.physicsBody = SKPhysicsBody(rectangleOfSize: chosenOne.frame.size)


        func didBeginContact(contact: SKPhysicsContact) {
            // 1. Create local variables for two physics bodies
            var firstBody: SKPhysicsBody
            var secondBody: SKPhysicsBody

            // 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
            if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
                firstBody = contact.bodyA
                secondBody = contact.bodyB
            } else {
                firstBody = contact.bodyB
                secondBody = contact.bodyA
            }

            // 3. react to the contact between ball and bottom
            if firstBody.categoryBitMask == GreenBallCategory && secondBody.categoryBitMask == RedBarCategory {
                //TODO: Replace the log statement with display of Game Over Scene
                println("Hit bottom. First contact has been made.")
            }
        }

I'm following http://www.raywenderlich.com/84341/create-breakout-game-sprite-kit-swift to the letter. I've got the self contact delegate all set up, the two do hit each other but it doesn't print to the logs to prove the collision stuff is actually working.

Any ideas?

Upvotes: 0

Views: 94

Answers (1)

ABakerSmith
ABakerSmith

Reputation: 22939

Your didBeginContact method is inside the didMoveToView method. Since both methods in SKPhysicsContactDelegate are optional:

protocol SKPhysicsContactDelegate : NSObjectProtocol {
    optional func didBeginContact(contact: SKPhysicsContact)
    optional func didEndContact(contact: SKPhysicsContact)
}

you're not being warned that GameScene doesn't implement the methods.

Move didBeginContact out of didMoveToView and you should be fine.

(On a side note: I would advise that you split up didMoveToView into several, smaller methods. It'll make it easier to understand what's going on because, at the moment, it looks pretty bloated.)

Upvotes: 2

Related Questions