How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection...

6
Step 6: Collision Detection How to Make a Game Like Flappy Bird in Swift

Transcript of How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection...

Page 1: How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection Implement SKPhysicsContactDelegate func didBeginContact(contact: SKPhysicsContact) {

Step 6: Collision Detection

How to Make a Game Like Flappy Bird

in Swift

Page 2: How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection Implement SKPhysicsContactDelegate func didBeginContact(contact: SKPhysicsContact) {
Page 3: How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection Implement SKPhysicsContactDelegate func didBeginContact(contact: SKPhysicsContact) {

Collision Detection

Implement SKPhysicsContactDelegatefunc didBeginContact(contact: SKPhysicsContact) { let other = contact.bodyA.categoryBitMask == PhysicsCategory.Player ? contact.bodyB : contact.bodyA if other.categoryBitMask == PhysicsCategory.Ground { hitGround = true } if other.categoryBitMask == PhysicsCategory.Obstacle { hitObstacle = true } }

Page 4: How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection Implement SKPhysicsContactDelegate func didBeginContact(contact: SKPhysicsContact) {

Game States

Various game states: Play Falling “Showing Score”

For each state: Different behavior when screen tapped Different things to do each frame

Actions when transitioning to a new state

enum GameState { case MainMenu case Tutorial case Play case Falling case ShowingScore case GameOver }

func switchToFalling() {

gameState = .Falling // Transition code… }

Page 5: How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection Implement SKPhysicsContactDelegate func didBeginContact(contact: SKPhysicsContact) {

Demo: Collision Detection

Page 6: How to Make a Game Like Flappy Bird in Swift · Like Flappy Bird in Swift. Collision Detection Implement SKPhysicsContactDelegate func didBeginContact(contact: SKPhysicsContact) {

Challenge