Simply put, I want to go from a spritekit Scene to a view in the Main Storyboard. It's easy to go from the main storyboard to a spritekit scene in swift. But I can't figure out how to go back to the storyboard. Thanks for the help. Cheers.
Initial viewController: an empty viewController with a button to present the GameViewController
GameViewController: the typical GameViewController of the "Hello World" Sprite-kit template. (This is a simplified version of the two scripts as of course you will have more code in yours, however, for the purpose of sharing what I did, this is easier)
My Goal: I wanted to present the first viewController from my SKScene game with the correct deallocation of my scene.
Description: To obtain the result I've extended the SKSceneDelegate class to build a custom protocol/delegate that make the transition from the GameViewController to the first initial controller (main menu). This method could be extended to other viewControllers of your game. This delegate is made use of in the return to main menu function. Make sure to put this function before you call the class for your spritekit script.
The two scripts are shown below. Hope this helps anybody else who had my question.
UIViewController:
import UIKit
import SpriteKit
class GameViewController: UIViewController,TransitionDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
scene.delegate = self as TransitionDelegate
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
func returnToMainMenu(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
guard let storyboard = appDelegate.window?.rootViewController?.storyboard else { return }
if let vc = storyboard.instantiateInitialViewController() {
print("go to main menu")
self.present(vc, animated: true, completion: nil)
}
}
}
Game Script:
import SpriteKit
protocol TransitionDelegate: SKSceneDelegate {
func returnToMainMenu()
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
self.run(SKAction.wait(forDuration: 2),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
self.view?.presentScene(nil)
(delegate as! TransitionDelegate).returnToMainMenu()
})
}
deinit {
print("\n THE SCENE \((type(of: self))) WAS REMOVED FROM MEMORY (DEINIT) \n")
}
}
Related
I am presenting a GKGameCenterViewController in an SKScene that inherits from the following protocol.
protocol GameCenter {}
extension GameCenter where Self: SKScene {
func goToLeaderboard() {
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = GameViewController()
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboard"
view?.window?.rootViewController?.present(vc, animated: true, completion: nil)
}
}
While the GKGameCenterViewController shows up perfect, when I try to dismiss by clicking the X in the top right corner, nothing happens. I assume this is because the reference to my original GameViewController has been deallocated. How can I get this dismissal to work?
According to Apple's Documentation:
Your delegate should dismiss the Game Center view controller. If your game paused any gameplay or other activities, it can restart those services in this method.
This means you need to implement the gameCenterViewControllerDidFinish method in your delegate and dismiss the gameCenterViewController yourself.
You should have something like this in your GameViewController()
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true, completion: nil)
}
In order to present the GKGameCenterViewController on a SKScene, I needed to find the currently displayed UIViewController reference and set this as the delegate. Here is the code I used and it works:
protocol GameCenter {}
extension GameCenter where Self: SKScene {
func goToLeaderboard() {
var currentViewController:UIViewController=UIApplication.shared.keyWindow!.rootViewController!
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = currentViewController as! GKGameCenterControllerDelegate
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboard"
currentViewController.present(vc, animated: true, completion: nil)
}
}
I'm using a ARView to show AR content in my app, but on dismissing the view controller, the memory is not completely deallocated.
I found this question, but I had no luck with the answer.
The memory consumption looks like this:
The entire codebase is very simple:
I've got a button that on pressing it, executes this:
let vc = SecondViewController()
self.present(vc, animated: true, completion: {
print("done")
})
and SecondViewController is a simple as this:
class SecondViewController: UIViewController {
var arView: ARView?
override func viewDidLoad() {
super.viewDidLoad()
self.arView = ARView(frame: self.view.frame)
self.view.addSubview(arView!)
arView!.automaticallyConfigureSession = true
}
deinit {
self.arView?.session.pause()
self.arView?.session.delegate = nil
self.arView?.scene.anchors.removeAll()
self.arView?.removeFromSuperview()
self.arView?.window?.resignKey()
self.arView = nil
}
}
But on dismissing, as can be seen in the memory graph, the memory is not deallocated fully.
I'm following a tutorial to create a "tetris" game in Swift with XCode 7. I followed every single step in this tutorial, but I'm getting a runtime error:
Could not cast value of type 'SCNView' (0x106c19778) to 'SKView' (0x1068fcad0).
My GameViewController.swift is as follows:
import UIKit
import SceneKit
import SpriteKit
class GameViewController: UIViewController {
var scene: GameScene!
var swiftris:Swiftris!
override func viewDidLoad() {
super.viewDidLoad()
//Configure the view
let skView = view as! SKView
skView.multipleTouchEnabled = false
//Create and configure the scene
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
scene.tick = didTick
swiftris = Swiftris()
swiftris.beginGame()
//Presente the scene.
skView.presentScene(scene)
scene.addPreviewShpaeToScene(swiftris.nextShape!){
self.swiftris.nextShape?.moveTo(StartingColumn, row: StartingRow)
self.scene.movePreviewShape(self.swiftris.nextShape!){
let nextShapes = self.swiftris.newShape()
self.scene.startTicking()
self.scene.addPreviewShpaeToScene(nextShapes.nextShape!) {}
}
}
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func didTick(){
swiftris.fallingShape?.lowerShapeByOneRow()
scene.redrawShape(swiftris.fallingShape!, completion: {})
}
}
I already search for it on google and here and I didn`t find anything related to SCNView and SKView.
Thank you in advance.
I did find the problem with my code. I instantiate the SKView as the view inside GameViewController:
self.view = SKView()
let skView = view as! SKView
Before I do that, I tried to change the class of the view to SKView in the storyboard, but it was not possible.
I'd like to thank you for the revisions.
I created a new Xcode Game project. I stripped the default animations in the project and added a new Swift file and a new .sks both called MenuScene.
In my GameViewController, I replaced references to by GameScene with MenuScene as I want this scene to launch first:
override func viewDidLoad() {
super.viewDidLoad()
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
if let scene = GKScene(fileNamed: "MenuScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! MenuScene? {
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
}
When I try to launch I get the following error:
Could not cast value of type 'SKScene' (0x1105624) to 'Spritekit10.MenuScene' (0x104c38).
If I change it back to GameScene, which appears to have the exact same setup, all seems fine.
Can anyone point me in the right direction with what the problem is with this?
On my MenuScene.sks under Custom Class, I've added MenuScene thinking that was the problem but the error still shows (and the textfield seems to randomly clear itself when I go back to it.
import SpriteKit
import GameplayKit
class GameScene: SKScene {
override func sceneDidLoad() {
}
}
.
import SpriteKit
import GameplayKit
class MenuScene: SKScene {
override func sceneDidLoad() {
}
}
So I figured out the issue.
For the .sks file, I had to add a name to the scene in the attributes inspector (even though it was blank in the GameScene). Once I done this, the custom class value saved and the file now loads without an error. Not sure if thats a bug or by design.
Your problem is you are trying to upcast an SKScene to a MenuScene.
This cannot be done, I see you have checked your custom class in your sks file to ensure that it is set to MenuScene, but did you make sure you saved it correctly? You need to type it in and hit enter, if you do not, then the scene will not auto save it, and when you leave will clear it out.
For Xcode 8.0/8.1 , create a swift file named "Menuscene" and an sks file named "Menuscene" too.
delete everything in the Menuscene swift file and insert this code
import SpriteKit
import GameplayKit
class Menuscene: SKScene {
override func didMove(to view: SKView) {
let menutext = SKLabelNode()
menutext.text = "MEnuscene"
menutext.position = CGPoint(x: 0, y: 0)
menutext.fontSize = 120
self.backgroundColor = UIColor.blue
addChild(menutext)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let gamescene = SKScene(fileNamed: "GameScene")
gamescene?.scaleMode = .fill
self.scene?.view?.presentScene(gamescene!,transition: SKTransition.doorsCloseHorizontal(withDuration: 1.0))
}
override func update(_ currentTime: TimeInterval) {
}
}
and in the attribute inspector tab, set name to "Menuscene", hit enter (very important). then on the next tab, set custom class name to "Menuscene",hit enter (very important).
In your Gamescene.swift file, double-check to be sure your class starts with
class GameScene: SKScene {
add this code to your touchesBegan func
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let Menuscene = SKScene(fileNamed: "Menuscene")
Menuscene?.scaleMode = .fill
self.scene?.view?.presentScene(Menuscene!,transition: SKTransition.doorsOpenVertical(withDuration: 1.0))
}
finally in your Gameviewcontroller file, delete everything and insert this code
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "Menuscene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override var prefersStatusBarHidden: Bool {
return true
}
}
you're welcome! you should be able to tap from game screen to menu screen.
I found something weird.
My project name has a "-" character.
After I removed this character from Target name, the project could cast scene.rootNode to GameScene.
It works on iOS 10.3.1 for me.
override func viewDidLoad() {
super.viewDidLoad()
let fileName = "GameScene"
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
guard let scene = GKScene(fileNamed: fileName) else {
return
}
guard let sceneNode = scene.rootNode as? GameScene ?? GameScene(fileNamed: fileName) else {
return
}
// Copy gameplay related content over to the scene
sceneNode.entities = scene.entities
sceneNode.graphs = scene.graphs
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
I have been trying to display adds when the game is over for my game app. I have created a game over variable on GameScene.swift and I call that variable in the GameViewController.swift. However, the add is displayed shortly after the game starts. Could anyone point out what am I doing wrong? Here is my code:
import UIKit
import SpriteKit
import GoogleMobileAds
private var interstitial: GADInterstitial?
class GameViewController: UIViewController, GADInterstitialDelegate {
private var interstitial: GADInterstitial?
override func viewDidLoad() {
super.viewDidLoad()
interstitial = createInterstitial()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func prefersStatusBarHidden() -> Bool {
return true
}
private func createInterstitial() -> GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "ca-app-pub-5713504307801022/7610056592")
interstitial.delegate = self
let request = GADRequest()
request.testDevices = ["ca-app-pub-5713504307801022/7610056592"]
interstitial.loadRequest(request)
return interstitial
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
interstitial = createInterstitial()
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
if (interstitial!.isReady) {
if gameOver1 == true {
self.interstitial!.presentFromRootViewController(self)
interstitial = createInterstitial()
}
}
}
}
First - this is how you should load and recycle AdMob interstitials -https://stackoverflow.com/a/37938286/2405378
Second - viewWillLayoutSubviews can be called multiple times and each time this method creates new instance of the GameScene - with gameOver1 being false?