Collision detection on SpriteKit of two objects
I am having trouble detecting a collision of two objects when I fire my cannon. The goal is to fire a bullet out of it and remove as soon as it cools down with an obstacle. There seems to be no collision in the game as I cannot get the "Collision" message I have set. Take a look at the following code:
class GameScene: SKScene, SKPhysicsContactDelegate {
enum BodyType: UInt32 {
case bullet = 1
case line = 2
case breaker = 4
}
I have listed the components, so I can install them in the Bitmask category. After that I made the setting when shooting when the touch starts.
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let bullet = SKSpriteNode(imageNamed: "bullet.png")
bullet.position = self.shooterTriangle.position
var actionRotate = SKAction.rotateToAngle(CGFloat(2 * -M_PI), duration: NSTimeInterval(0.1))
let actionMove = SKAction.moveTo(CGPoint(x: 500, y: size.height * 0.5), duration: NSTimeInterval(0.5))
let removeBullet = SKAction.removeFromParent()
bullet.runAction(SKAction.sequence([actionRotate]))
bullet.runAction(SKAction.sequence([actionMove, removeBullet]))
addChild(bullet)
// Bullet Physics Start ****
bullet.physicsBody? = SKPhysicsBody(circleOfRadius: bullet.size.width/2.0)
bullet.physicsBody?.dynamic = true
bullet.physicsBody?.affectedByGravity = false
bullet.physicsBody?.allowsRotation = false
bullet.physicsBody?.categoryBitMask = BodyType.bullet.rawValue
bullet.physicsBody?.contactTestBitMask = BodyType.line.rawValue
bullet.physicsBody?.collisionBitMask = BodyType.line.rawValue
}
}
Then I'll add the line, I want the bullet to match the loadGameComponents function ...
func loadGameComponents() {
//Load line....
let line = SKSpriteNode(imageNamed: "Line_1.png")
line.position = CGPoint(x: size.width * 0.95, y: size.height * (2.0))
addChild(line)
var actualSpeedDuration = 2.2
let lineActionMove = SKAction.moveTo(CGPoint(x: size.width * 0.95, y: size.height * 0.6), duration: NSTimeInterval(actualSpeedDuration))
line.runAction(SKAction.sequence([lineActionMove]))
// Line Physics starts! *****
line.physicsBody? = SKPhysicsBody(rectangleOfSize: CGSize(width: line.size.width, height: line.size.height))
line.physicsBody?.dynamic = false
line.physicsBody?.affectedByGravity = false
line.physicsBody?.allowsRotation = false
line.physicsBody?.categoryBitMask = BodyType.line.rawValue
line.physicsBody?.contactTestBitMask = BodyType.bullet.rawValue
line.physicsBody?.collisionBitMask = 0
Finally, I defined contactDelegate = self in my didLoad function and included didBeginContact method
func didBeginContact(contact: SKPhysicsContact) {
println("Contact!!!")
}
I don't get the message at all, but I'm not sure what is going wrong!
Thank you for your patience!:)
source to share
It looks like the main problem is what I used to indicate an optional indicator (?) When declaring the physical body of the sprite. the correction should be done on these lines ...
line.physicsBody? = SKPhysicsBody(rectangleOfSize: CGSize(width: line.size.width, height: line.size.height))
bullet.physicsBody? = SKPhysicsBody(circleOfRadius: bullet.size.width/2.0)
Removing them solved my problem :)
I also included view.showPhysics = true
didMoveToView in my method, so it's easy to see the borders between the sprites.
source to share