I'm trying to let the player jump. However, I cannot let him jump in the air, so I need to check if he should stand on top of another node before applying the impulse.
How do other sprite games usually do this?
Now I'm trying two approaches, the first of them is this one. This is great, however, I donβt know how to determine if the node the player really touches is actually below or to the side of the player.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { var allowJump = false let bodies = player.physicsBody.allContactedBodies() for body : AnyObject in bodies { if let node = body.node { allowJump = true
This is a different approach, it is effective to find out if the contact has started, and also whether the contact is really with the ground (under the player), and not with the wall or such. However, I do not know how to say when the player ceases to be on top of any nodes, because even if the contact ends, he can still contact another node.
func didBeginContact(contact: SKPhysicsContact!){ if(contact.bodyA.node.position.y < contact.contactPoint.y) { println("BodyA is on top of another node") } if(contact.bodyB.node.position.y < contact.contactPoint.y) { println("BodyB is on top of another node") } } func didEndContact(contact: SKPhysicsContact!){ if(contact.bodyA.node.position.y < contact.contactPoint.y) { println("BodyA is on no longer on top of BodyB") } if(contact.bodyB.node.position.y < contact.contactPoint.y) { println("BodyB is no longer on top of BodyA") } }
source share