I kinda struggling to sort this problem out, I have created same methods to detect which is the box but somthing is still ain't right here this is my code:
If someone could help me and share some code it will be much appreciated
Where I create the box I have added " box.physicsBody?.contactTestBitMask = box.physicsBody!.collisionBitMask"
I kind of declared same methods but is still not working.
let size = CGSize(width: Int.random(in: 16...128), height: 16)
let box = SKSpriteNode(color: UIColor(red: CGFloat.random(in: 0...1), green: CGFloat.random(in: 0...1), blue: CGFloat.random(in: 0...1), alpha: 1), size: size)
box.physicsBody?.contactTestBitMask = box.physicsBody!.collisionBitMask
box.zRotation = CGFloat.random(in: 0...3)
box.position = location // add the box object were the user taps
box.name = "box"
box.physicsBody = SKPhysicsBody(rectangleOf: box.size)
box.physicsBody?.isDynamic = false // box object will not move when impact happens
addChild(box) // box object will be added to the screen
func boxandballCollision(box: SKNode, ball: SKNode) {
if ball.name == "ball" {
destroyObject(box: box)
} else if ball.name == "ball" {
destroyObject(box: box)
}
}
func destroyObject(box: SKNode) {
if let fireParticles = SKEmitterNode(fileNamed: "FireParticles") {
fireParticles.position = box.position
addChild(fireParticles)
}
box.removeFromParent() // box should be removed when a ball will hit it
}
func begin(_ contact: SKPhysicsContact) {
guard let nodeA = contact.bodyA.node else { return }
guard let nodeB = contact.bodyB.node else { return }
if nodeA.name == "box" {
boxandballCollision(box: nodeB, ball: nodeB)
} else if nodeB.name == "box" {
boxandballCollision(box: nodeA, ball: nodeA)
}
}
}