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.
Related
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?
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!
(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!
I have the following setup:
A UIPageviewController that has two child-ViewControllers.
ViewController 1 contains a UITableView, ViewController 2 holds a camera setup (AVCaptureSession, etc). It's basically a Snapchat like setup.
Swiping between the two works perfectly - swipe left, UITableView is showing, swipe right and the camera shows up again. So far so good.
Now I want to implement a button that turns the page to get to the controller that hold the UITableView programatically. Here's how I do it:
let startingViewController = self.viewControllerAtIndex(0)
let viewControllers: [UIViewController] = [startingViewController!]
pageViewController!.setViewControllers(viewControllers, direction: .Reverse, animated: true, completion: nil)
This works for the most part, as it slides out the UIViewController holding the camera instance and slides in the one holding the UITableView. However, it seems like the view controller that holds the camera session is deallocated, so when I swipe right again, the camera has to be fired up again. Is there any way to turn the page differently, without having the camera session deallocated?
I have tried adding the camera to the PageViewcontroller and adding an "invisible" overlay, but this is not the solution I am looking for. Can someone help out here?
The correct way to do this is using a UIScrollView with multiple subviews and paging enabled. When you press the button, you do not "turn the page", you manipulate/animate the content of the scroll view.
For further reference:
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html
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.