0

I want to know how to change to a scene when the character collides with the enemy at game over. I have made a scene under main.storyboard and I want to know how to hook it up through code, I only know how to hook it up using buttons but thats not what I am looking for as you would not press a button when you die to take you to the game over scene.

UPDATE:

func gameOver() {
        gameDelegate?.gameDelegateGameOver(score)


        let gameOverScene: GameOverScene = GameOverScene(size: self.size)
        self.view!.presentScene(gameOverScene, transition: SKTransition.fadeWithDuration(0.0))

Thats what I have for my gameOver when collision is detected. Yes it does take me to a new scene but not the scene I made in main.storyboard.

Swift101
  • 81
  • 10

2 Answers2

1

What you could do is to create a collision boolean and if it's true (hence, something has collided), you can present a new scene with view.presentScene(YOUR_SCENE, SK_ANIMATION) in the update function.

EDIT:

I've found the tutorial from where you got the code (or at least I assume you did) and got it working with the following:

In "didMoveView" add:

player.physicsBody?.categoryBitMask = PhysicsCategory.Player
player.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
player.physicsBody?.collisionBitMask = PhysicsCategory.None
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width/2)
player.physicsBody?.dynamic = true

(PhysicsCategory.Player is just a value I added in the PhysicsCategory)

Then, in the function where you add the enemy sprites you have to add in order to make the two collide:

monster.physicsBody?.contactTestBitMask = PhysicsCategory.Player

Last but not least, you have to add the following code to add "an action" to the collision the didBeginContact function:

if ((secondBody.categoryBitMask & PhysicsCategory.Monster != 0) &&
    (firstBody.categoryBitMask & PhysicsCategory.Player != 0)) {
        let gameOverScene = GameOverScene(size: self.size, won: false)
        view?.presentScene(gameOverScene, transition: SKTransition.flipHorizontalWithDuration(0.5))
}

Hope it's working for you now!

Julia Grill
  • 93
  • 2
  • 9
0

If you want to present a storyboard scene, you need to use something like

let gameOverScene = self.storyboard!.instantiateViewControllerWithIdentifier("GameOverViewController") as! GameOverViewController
self.view!.presentScene(gameOverScene, transition: SKTransition.fadeWithDuration(0.0))
Daniel K
  • 1,119
  • 10
  • 20