2

I am having trouble figuring out how to change view controllers when your player collided with an object.

I want to like a menu to pop-up displaying a menu button and a replay button, also so extra buttons that are not important at this moment of time. I am not sure how some of those end of game menus are made, I am thinking switching view controllers, if you know exactly how they are made please tell me.

This is the code I have at the moment, and the only thing it does is display a label that the game is over and when that label is tapped the game will restart:

import Foundation
import AVFoundation
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var movingGround: PPMovingGround!
var square1: PPSquare1!
var wallGen: PPWallGen!
var diamondGen: PPDiamondGen!

var isStarted = false
var isGameOver = false
var isDiamondContact = false

var playerNode: SKNode!

override func didMoveToView(view: SKView) {

//code that is not important was deleted
func collisionWithDiamond() {
    isDiamondContact = true 
}

func restart() {

    let newScence = GameScene(size: view!.bounds.size)
    newScence.scaleMode = .AspectFill

    view!.presentScene(newScence)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if isGameOver {
        restart()

    } else {
        square1.flip()
    } 
}

override func update(currentTime: CFTimeInterval) {

}

// MARK: - SKPhysicsContactDelegate
func didBeginContact(contact: SKPhysicsContact) {

    if !isGameOver {
        gameOver()
    } else {
        !isDiamondContact
        collisionWithDiamond()
    } 
}

Note: I have deleted code that is unrelated or not necessary.


Updates:

Link to a game play of a game: https://www.youtube.com/watch?v=WUibTETfEQY

SKIP TO 2:32 TO SEE THE GAME OVER SCREEN

Link to image of game over screen: Image

(I was unable to post an image here because I don't have the required 10 rep points yet.)

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Swift101
  • 81
  • 10

2 Answers2

1

// Edited Answer

This will be the easiest. Create a new GameOverScene.swift that is a SKScene. Then customize that scene however you want with background image, SKLabelNodes for buttons. Checkout creating buttons in skview to point to different scenes

When the game ends in GameScene,

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

Here is a project that has this implemented, http://www.raywenderlich.com/76741/make-game-like-space-invaders-sprite-kit-and-swift-tutorial-part-2

// First Answer -----------------------------------------------

If you want to switch viewControllers, you will have to present the new viewController like this or with segue,

self.view?.window?.rootViewController?.presentViewController(newView, animated: true, completion: nil)

self.view?.window?.rootViewController?.performSegueWithIdentifier("id", sender: AnyObject)

Otherwise create a SKView and add buttons, then add it to the scene when game is over or add it before, hide it, then show it. Once user picks a choice, remove it or hide it with,

SKView.hidden = false
SKView.hidden = true

Add SKView with,

self.view?.addSubview(SKView)

Simple SKView overlay,

let view1 = SKView(frame: CGRectMake(0, 0, 200, 200))
view1.center = self.view!.center
self.view?.addSubview(view1)
Community
  • 1
  • 1
Rasputin
  • 487
  • 4
  • 10
  • I have added a link to a game over screen to help you understand my question and I also added an image along with it – Swift101 Jun 26 '15 at 19:28
  • Do I need to make a new Cocoa Touch Class and select a subclass of SKView or SKScene? – Swift101 Jun 26 '15 at 19:37
  • You don't have to. Here is a simple SKView overlay. Answer Edit – Rasputin Jun 26 '15 at 19:39
  • Are you able to show me an example of you inputting the code you posted above with my code, as I am kinda new to swift and I am not familiar with some stuff. If you have the time to do that it will be greatly appreciated! – Swift101 Jun 26 '15 at 19:43
  • also do I have to add the SKView overlay to my gameScene or my gameSceneViewController? – Swift101 Jun 26 '15 at 19:48
  • 1
    I edited my answer with a link to a game tutorial that uses another scene as a end game scene, which you can customize to have more SKNodes as buttons that do different things. Also, linked is how to make SKNodes be like buttons. – Rasputin Jun 26 '15 at 20:31
  • Thanks! Alot for the perfect answer man, you really helped me out a lot have a grate day!! – Swift101 Jun 26 '15 at 20:34
0

If the game is over and you want to present a selective menu, you could present a UIAlertView that presents the user with whatever options that you want. From there they could restart the game, or they could choose to go to some other view like one that manages their player stats or something ( I don't know what exactly your game is).

pbush25
  • 5,228
  • 2
  • 26
  • 35
  • I want a custom game over pop up thing, I will update my post with a picture and a link of a game play of the game, so then you are able to see exactly I am wondering how to make. – Swift101 Jun 26 '15 at 19:18