Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can i detect collision in Sprite Kit using Swift?

This is my code

func didBeginContact(contact: SKPhysicsContact) {

    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch (contactMask){

    case BodyType.ball.toRaw() & BodyType.hole.toRaw():

        println("HEY")

    default :
        return
    }

}

//it prints nothing

//the declarations:

func addHole(#size : CGSize) {

    let actionMoveDown = SKAction.moveToY(CGRectGetMidY(self.frame)-500, duration: 4.7)
    let hole = shapedHoles()
    let UT = UTIL()
    var position:CGFloat


    let randomPosition = UT.randomNumberWith(Min: 1, Max: 3)

    switch randomPosition{
    case 1:
        position = CGRectGetMidX(self.frame)
    case 2:
        position = CGRectGetMidX(self.frame)+size.width

    default:
        position = CGRectGetMidX(self.frame)-(size.width)

    }



    var createdHole = hole.createHoleAtPosition(position: CGPointMake(position ,CGRectGetMaxY(self.frame) + (size.height/2)),size: size )//CGSizeMake(CGRectGetMaxX(self.frame)/3 - 10, 70)

    createdHole.physicsBody = SKPhysicsBody(rectangleOfSize: createdHole.frame.size)
    createdHole.physicsBody?.categoryBitMask = BodyType.hole.toRaw()
    createdHole.physicsBody?.contactTestBitMask = BodyType.ball.toRaw()
    createdHole.physicsBody?.affectedByGravity = false
    createdHole.physicsBody?.dynamic = false


    lastHolePosition = randomPosition

    createdHole .runAction(actionMoveDown)

    self.addChild(createdHole)



}

func addSphere(){
    let mainCharacterController = circle()
    let character: (SKNode) = mainCharacterController.createCircleAtPosition(position: CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame)+100))


    character.physicsBody = SKPhysicsBody(circleOfRadius: character.frame.size.height/2)
    character.physicsBody?.categoryBitMask = BodyType.ball.toRaw()
    character.physicsBody?.contactTestBitMask = BodyType.hole.toRaw()
    character.physicsBody?.affectedByGravity = false
    character.physicsBody?.dynamic = false


    self.addChild(character)


}