How can I do something when contacts between two lines in my game occur? Everytime a touch on the screen occurs this code runs and a new line is spawned.
func spawnLine() {
let constantGravityVector = vector_float3(x: 0, y: -15, z: 0)
let constantGravityNode = SKFieldNode.velocityField(withVector: constantGravityVector)
print("Drop")
// Create line
let line = SKSpriteNode(imageNamed: "Line")
line.position = CGPoint(x: self.size.width/2, y: self.size.height * 0.8)
line.zPosition = 1
line.name = "line"
line.physicsBody?.affectedByGravity = false
line.physicsBody?.isDynamic = true
line.physicsBody?.categoryBitMask = PhysicsCategory.LineContact
line.physicsBody?.collisionBitMask = PhysicsCategory.CircleContact
line.physicsBody?.contactTestBitMask = PhysicsCategory.CircleContact | PhysicsCategory.LineContact
line.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: line.size.width, height: line.size.height))
line.addChild(constantGravityNode)
self.addChild(line)
}
As of now this is how I have the code to test if a contact occurs.
func didBegin(_ contact: SKPhysicsContact) {
if contact.bodyA.node?.name == "line" && contact.bodyB.node?.name == "line" {
print("Line Contact")
}
if contact.bodyA.node?.name == "line" && contact.bodyB.node?.name == "circle" {
print("contact a")
// Joint the line to the circle
scene?.physicsWorld.add(SKPhysicsJointFixed.joint(withBodyA: contact.bodyA, bodyB: contact.bodyB, anchor: contact.contactPoint))
}
else if contact.bodyB.node?.name == "line" && contact.bodyA.node?.name == "circle" {
print("contact b")
// Joint the line to the circle
scene?.physicsWorld.add(SKPhysicsJointFixed.joint(withBodyA: contact.bodyA, bodyB: contact.bodyB, anchor: contact.contactPoint))
}
}
This code works for for checking between a line and circle. However, I have the lines 5-7 to test if contact between line SKSpriteNodes occurs, but it doesn't work. So, how do I get this part of the code to run something when two of the line SKSpriteNodes come into contact?
Thanks for any help.