I'm re-using the approach used in DemoBots for contact detection. I've come across a problem because of a difference between how my code works and how DemoBots is written.
In DemoBots, the component RenderComponent has a variable 'node
'
let node = EntityNode()
init(entity: GKEntity) {
node.entity = entity
}
In my code, I'm not using animations, so my equivalent of RenderComponent is a bit more complex. The variable 'node' is already used and tied to the physicsBody, so I've created a new variable 'entityNode
'
let node: SKSpriteNode
let entityNode = EntityNode()
init(entity: GKEntity, nodeTemplate: SKSpriteNode) {
node = SKSpriteNode(imageNamed: "animal")
entityNode.entity = entity
}
Within the handleContact: method, I've tried two solutions to get the reference to the entity. Option 1:
let entityA = (contact.bodyA.node as? EntityNode)?.entity
let entityB = (contact.bodyB.node as? EntityNode)?.entity
Running a print() check on this returns a nil value for these two entities. That's logical enough as I should look for a property named 'entityNode
' to find the entity. When I change it to
Option 2:
let entityA = (contact.bodyA.entityNode as? EntityNode)?.entity
let entityB = (contact.bodyB.entityNode as? EntityNode)?.entity
I get an error that the physicsBody doesn't have a member 'entityNode
.' I've tried a bunch of other things like looking up the entity associated with the contactBody, but I get another error which says the subsequent code is looking for an optional. I've tried getting rid of the 'if' but it just moves the problem along to the next contactCallBack
call.
if let notifiableEntity = entityA as? ContactNotifiableType, otherEntity = entityB where aWantsCallback {
contactCallback(notifiableEntity, otherEntity)
}
Any ideas how I can resolve this to get the contactCallBack
call working?