Skip to content

Instantly share code, notes, and snippets.

@lucasprag
Created January 29, 2016 12:03
Show Gist options
  • Save lucasprag/48f8257e86791033cd85 to your computer and use it in GitHub Desktop.
Save lucasprag/48f8257e86791033cd85 to your computer and use it in GitHub Desktop.
The power of the physics body
// omitted
class GameScene: SKScene {
// omitted
func setupPlayer() {
// omitted
// we create a physics body with the size of the player
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
// yes, it's that simple to be affected by gravity
player.physicsBody?.affectedByGravity = true
// make the player collide with the floor
player.physicsBody?.categoryBitMask = Physics.Character
player.physicsBody?.collisionBitMask = Physics.Character
// this add the player to the scene in order to appear in the screen
addChild(player)
}
func setupFloor() {
// omitted
// we create a physics body with the size of the floor
floor.physicsBody = SKPhysicsBody(rectangleOfSize: floor.size)
// the floor doesn't move, right? =)
floor.physicsBody?.affectedByGravity = false
// make the floor collide with the player
floor.physicsBody?.categoryBitMask = Physics.Character
floor.physicsBody?.collisionBitMask = Physics.Floor
// this add the floor to the scene in order to appear in the screen
addChild(floor)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment