Go to Gamescene back to viewcontroller - swift

I have a simple game. there are two Viewcontroller. after pressing "play" on the first viewcontroller i have performed a segue to the secondviewcontroller. this secondviewcontroller contains a GADBannerView and a button to stop the game and presents a Gamescene. if the user loses it shows up a new game scene. if the user clicks on the GameoverScene the game restarts. I would like to to perform a segue after the user press on the button and i would like to go back to the first Viewcontroller. I've made it in the storyboard. the problem is that the game doesn't stop. (i know it because i have a background music) and once i'm in the first viewcontroller if I click again "play" the game doesn't work as it should. i've figured that the problem occurs when i click the "back button". how can I dismiss the game scene and go back to the first viewcontroller?

There's no way to tell what your problem is, because you didn't post any code, but you can try something like this to dismiss your Game Scene:
self.view?.presentScene(nil) // From your Game Scene!
I have a game, and I use a method that does that job, and it looks something like this:
class GameScene : SKScene {
// ...
func dismiss() {
self.removeAllActions()
self.removeAllChildren()
self.removeFromParent()
self.view?.presentScene(nil)
}
// ...
}
After that, call the method that will present your ViewController!
Hopefully that's useful for you!

Related

Segue from SKScene and back again cause game to "freeze"

I'm developing a SpriteKit game with two different scenes (MainMenu.sks and Settings.sks). I also have a UIViewController.
From the MainMenu the user can tap "Settings" which causes the app to load the Settings.sks scene and present it.
It's done using this code:
// Move to Settings.sks
let transition = SKTransition.flipVertical(withDuration: 1.0)
let loadScene = SettingsScene(fileNamed: "SettingsScene")
loadScene?.scaleMode = self.scaleMode
self.view?.presentScene(loadScene!, transition: transition)
From the Settings.sks scene the user can tap on the SKLabelNode "Change username" which causes the app to load the UIViewController by performing a segue.
The UIViewController contains a UITextField and a UIButton. The UIButton takes the user back to MainMenu.sks.
However, when I'm doing so the it's like a new scene of MainMenu is placed upon the other causing the app to sort of "freeze" and not responding to any touch gestures. I can see from the nodeCount that the nodes are doubled.
A workaround is loading the UIViewController directly from the MainMenu.sks (also via a segue). By doing so, the app works just fine.
I'm suspecting the problem to be the way I load Settings.sks.
What am I missing? Maybe unloading the entire SKView before exiting?
Performing a segue will present a new view on top of the stack. By using a segue to return to the main menu, the app will create a new main menu on top of the UIViewController. I can't say for certain why your app freezes. But I would guess that there are some properties or functions that don't work properly when a new main menu is created and presented from your UIViewController.
I recommend unwinding the segue used to reach the UIViewController instead. This way you can return to the previous view and conserve memory.
You'll need to write an IBAction in your destination view, the main menu in this case:
#IBAction func unwindToMainMenu(segue: UIStoryboardSegue) {
}
Then ctrl-drag from your button in the UIViewController to the exit icon in storyboard. You should see an option to use the IBAction unwindToMainMenu that you just wrote. This will create an unwind segue.
A complete guide to unwind segues and multiple examples can be found in this answer: What are Unwind segues for and how do you use them?

Swift View Controller won't dismiss

so I have a basic game going in Xcode. I started with Xcode's basic game template and built my game mostly inside the 'GameScene.swift'. When I finished with my game I wanted to add a start screen with a play button so I made a new UIViewController and added a button. I control click and dragged the button to the GameViewController and created a Modal Segue. I then wanted the game to go back when the player died. I tried various ways to dismiss the view but none worked. I am new to swift and really need help. Let me know what code/info is needed to find a solution. I have tried everything I have found on the internet. I thought I found a way around it by adding a button and Segue to the GameViewController to the menu but after multiple pushed it clogged the system and slowed to a crawl because none were dismissed. I can provide any code needed.
Inside of GameViewController.swift in the GameViewController class I tried making a function that was called when the game ended.
I tried to both pop and dismiss the view controller. The function was called and a line printed to console but the view remained. The only thing printed to console is 'nil'
class GameViewController: UIViewController {
func end(){
print(navigationController?.viewControllers as Any)
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
}
# Vollan heres the pic
Here is the screenshot of the storyboard.
thanks in advance.
If you pushed the viewController you use self.navigationController?.popViewController(animated: true)
If you presented it modally you use
self.dismiss(self, animated: true)
When its presented from a modal segue you use
self.presentingViewController?.dismiss(animated: true, completion: nil)
I have a modal segue in a storyboard from one view controller to the other. Then I have a button that's connected to an IBAction that just runs
dismiss(animated: true, completion: nil)
Check and redo the connections from the storyboard to the code. Xcode sometimes just looses a connection when making a lot of changes to the code.

Using manuel segues with Swift Spritekit

(Swift) I have a problem with my app, I'm working on. It features a SpriteKit Scene and some transitions to different scenes. However, from the main menu of the app, I've created a segue to a new viewController via Story Board.
My problem is, when I - in my sprite kit scenes, browse back to the main menu to trigger the Story Board scene my app crashes.
I can only use the Story Board segue if restart my App and triggers it from the main menu as the first thing.
Also, when I get back to my Sprite Kit Scene from the new viewController, I cannot use any of the nodes (as buttons) to make transitions to other sprite kit scenes.
I've tried to illustrate it here:
Code for calling the segue:
func goto_achievements() {
self.viewController?.performSegueWithIdentifier("achievements", sender: self)
}
Any help appreciated!

How do I use storyboards with spriteKit using swift

One of my main reason for using Xcode instead of other applications for making iOS apps was the storyboard interface builder. I was unhappy when I found out that you are not meant to use storyboards with spriteKit. I find it hard to design a nice interface for a game's menu without a good visual builder. Is there a way to start a spriteKit application using storyboards, then with a click of a "start game" button, switch to spriteKit scenes, then when you lose the game, in the code switch back to storyboards(using swift)? Please help and thanks.
-Callum-
A SpriteKit Scene is presented on an instance of a SKView, which is a subclassed UIView.
For an iOS game made using SpriteKit, one needs to have at least one viewController set up, either programatically in the App delegate or in a storyboard, on which the SKScene can be displayed. It is on the main view of this VC that a SKScene will be presented in.
So if you are using a storyboard, the iOS game will have to instantiate the root viewController from it. You can easily design your UI on the viewController, and present the game from the code on the press of a button, either in the same viewController or a new one. All this will be evident once you read a beginner tutorial for SpriteKit using Swift like this.
Let's say your root viewController has your main menu (on another view called menuView), with a play button on it. Now presenting a game on the press of a button would look something like this:
class MyViewController: UIViewController {
#IBOutlet wear var menuView: UIView?
#IBOutlet weak var playButton: UIButton?
#IBAction func likedThis(sender: UIButton) {
//Hide the menu view
menuView.hidden = true
//instantiate and present the scene on the main view
let scene = MyScene(size: view.bounds.size)
let skView = self.view as SKView
skView.presentScene(scene)
}
}
As for going back to the main menu from the scene, have a look at this answer.

Removing an adBannerView in Swift (SpriteKit)

When the user crashes and the game over code block is run the adBannerView appear at the top of the screen, when the user taps to play again it dissapears using the removeFromSuperView()
However, if the user goes to the "Settings Scene" (ad banner view dissapears and when the settings scene loads it loads a new bannerView ect) but when i hit the "Play" Button (which takes the user back to the gameScene the adBannerview doesn't get removed
In my game Scene:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
adBannerView.removeFromSuperview()
}
contactBegins {
//GameOver
loadAds()
}
// Pressed Settings Node
adBannerView.removeFromSuperview()
In settings i have exactly the same setup then when the user presses "Play" it has removeFromSuperView but nothing gets removed when it transitions to the next scene...
When your move to the next scene just make the banner hidden like this:
adBannerView.hidden = true
Also, look through your code and make sure you're not re-adding the banner in the game scene as that could be the problem too.