Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Swift SpriteKit Collision Detection

import SpriteKit class Scene1: SKScene, SKPhysicsContactDelegate { let collisionBulletCategory: UInt32 = 0x1 << 0 let collisionHeroCategory: UInt32 = 0x1 << 1 let spet = SKSpriteNode(imageNamed: "batdinger") let spets = SKSpriteNode(imageNamed: "batdinger") override func didMoveToView(view: SKView) {

self.physicsWorld.contactDelegate = self
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
// 2. Set the friction of that physicsBody to 0
borderBody.friction = 0
// 3. Set physicsBody of scene to borderBody
self.physicsBody = borderBody

physicsWorld.gravity = CGVectorMake(-5, 0)
spets.physicsBody? = SKPhysicsBody(rectangleOfSize: spet.size)
spets.physicsBody?.categoryBitMask = UInt32(collisionHeroCategory)
spets.physicsBody?.dynamic = true
spets.physicsBody?.contactTestBitMask = UInt32(collisionHeroCategory)
spets.physicsBody?.collisionBitMask = 0x0
spets.physicsBody?.allowsRotation = false
spets.physicsBody?.affectedByGravity = false
spets.physicsBody?.friction = 0
spets.physicsBody?.restitution = 1
spets.physicsBody?.linearDamping = 0
spets.physicsBody?.angularDamping = 0
spets.position = CGPointMake(300, 500)
spet.physicsBody? = SKPhysicsBody(rectangleOfSize: spet.size)
spet.physicsBody?.categoryBitMask = UInt32(collisionBulletCategory)
spet.physicsBody?.dynamic = true
spet.physicsBody?.contactTestBitMask = UInt32(collisionBulletCategory)
spet.physicsBody?.collisionBitMask = 0x0
spet.physicsBody?.allowsRotation = false
spet.physicsBody?.affectedByGravity = false
spet.physicsBody?.friction = 0
spet.physicsBody?.restitution = 1
spet.physicsBody?.linearDamping = 0
spet.physicsBody?.angularDamping = 0
spet.position = CGPointMake(200, 300)
spet.physicsBody?.dynamic = true

// spet.physicsBody = SKPhysicsBody(texture: heroSprite.texture, size: heroSprite.size) spet.physicsBody?.affectedByGravity = false spet.physicsBody?.categoryBitMask = collisionHeroCategory spet.physicsBody?.collisionBitMask = 0x0 let poso = CGPointMake(400, 200) let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1) // let mover = SKAction.followPath(x: 150, y: 300, duration: 1.5) let mr = SKAction.moveTo(poso, duration: 2.0) spets.runAction(SKAction.repeatActionForever(mr))

//spet.xScale = 0.5
//spet.yScale = 0.5
addChild(spet)
addChild(spets)
backgroundColor = UIColor.blueColor()
}

func didBeginContact(contact: SKPhysicsContact)
{
    println("HI")
}

   override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
    spet.position = CGPointMake(400, 200)
    let scene = GameScene(size: self.scene!.size)
    scene.scaleMode = SKSceneScaleMode.AspectFill

        //self.scene!.view!.presentScene(GameScene(fileNamed:  "GameScene"), transition: transition)

}

}

I have searched google and any other sources and tutorials for a few hours now, followed them exactly, and even pasted source code from another project to my project, and my collision detection wont work. What's wrong? Is it my code wrong? Am I not calling the 'didBeginContact' function correctly? Please help!