Jeremy
Jeremy

Reputation: 63

how to present a uiviewcontroller over a spritekit scene

Hi I have seen many questions about this topic but none seem to solve my situation. I want to present a UIView controller over an SKScene when the user win/dies. I created a PresentWinLoseViewController : UIViewController subclass and connected it in story board. I created a segue from GameViewController to PresentWinLoseViewController in storyboard plus two exit segues from PresentWinLoseViewController back to GameViewController.

Here is how I am calling these. In my SKScene when I win/lose

func win() {
     let gameVC = GameViewController()
    gameVC.performSegueWithIdentifier("tryAgain", sender: self)
}

and my exit segues from PresentWinLoseViewController

   @IBAction func tryAgainButtonPressed(segue:UIStoryboardSegue) {

    self.performSegueWithIdentifier("unwindTryAgain", sender: self)

}

but I cant even get that far bc the first segue never works. I keep getting

Receiver (<SpriteLevelBuilder.GameViewController: 0x7ff2b5e49210>) has no segue with identifier 'tryAgain''

what am i doing wrong?

Upvotes: 0

Views: 1003

Answers (2)

Stewart Hering
Stewart Hering

Reputation: 304

I would do this by placing the elements that you would like to come on over top your scene in the same view controller just on a level above the one with your scene. Using a UIImage View is a great way to make the background for the new elements. Then just create a function for showing the elements and one for hiding them. For all UI elements .hidden = true would be the way to hide the elements and .hidden = false would be the way to show the elements. Here is an example of some code in one of my apps.

//this is the function that hides the settings menu
func hideSettingsMenu() {
    settingsCoverImage.hidden = true
    settingsTitle.hidden = true
    watchButton.hidden = true
    fitBitButton.hidden = true
    backButton.hidden = true
    classesButton.hidden = true
    settingsTopBackground.hidden = true
}

//this is the function that shows the settings menu
func showSettingsMenu() {
    settingsCoverImage.hidden = false
    settingsTitle.hidden = false
    watchButton.hidden = false
    fitBitButton.hidden = false
    backButton.hidden = false
    classesButton.hidden = false
    settingsTopBackground.hidden = false
}

By calling those functions It simply shows or hides the elements for that page. I am currently working on animating the transitions between pages. Doing it this was also gives you the option to add transparency, giving your app good looks ;)

Upvotes: 0

Kelvin Lau
Kelvin Lau

Reputation: 6771

For starters, GameViewController should already be initialized, there is no need to let gameVC = GameViewController()

This is because your SKScene is actually presented by your GameViewController. Go look in your GameViewController and you should see the initialization process of SKScene in the viewDidLoad().

Since the GameViewController is already initialized, all you need to do is directly call performSegueWithIdentifier on the GameViewController from your SKScene.

I just did a quick search and this answer seems to apply to your situation: How to call method from ViewController in GameScene

Upvotes: 1

Related Questions