Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gamescene.swift and Gamescene.sks not working together

I am having a problem with my xCode project. I have a whole gamescene.swift file worth of code (600 lines). But the code will not run. When i run my game in simulator the simulator shows the standard gamescene.sks color, alongside with my ads, but it is not showing any of my code. I have already checked that the custom class in gamescene.sks is correct. Everything is working, expect for the gamescene.swift code, it will not run.

import UIKit
import SpriteKit
import GameplayKit
import GoogleMobileAds

class GameViewController: UIViewController {


@IBOutlet weak var GoogleBannerView: GADBannerView!
@IBOutlet weak var MainMenu: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()

    GoogleBannerView.adUnitID = "ca-app-pub-13***46014918193/236762****"
    GoogleBannerView.rootViewController = self
    GoogleBannerView.load(GADRequest())


    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .resizeFill

            // Present the scene
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

        view.showsFPS = false
        view.showsNodeCount = false
        view.showsPhysics = 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
}

}

like image 897
slindten Avatar asked Feb 16 '17 09:02

slindten


3 Answers

i managed to fix the problem after countless hours of trying.

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = GameScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .resizeFill

this is the working code. the problem was that instead of specifying what scene i was suppose to lead i had written

if let scene = SKScene(fileNamed: "GameScene")

the simple fix was changing the "SKScene" to my SKScene name.

if let scene = GameScene(Filenamed: "GameScene") 

Hope this helps the countless other out there that are having trouble with this problem!

like image 106
slindten Avatar answered Nov 18 '22 06:11

slindten


I've had similar problems to this on SOME of my projects but not all. Even on projects that were working and I duplicated the code and scene files for another project they would stop working. I discovered that if there are any spaces or special characters in the your project name the scene files and items in the sks files will not load unless you put in the "Module" name below the Custom Class type.

For me my project was named "Crag & Pig" before any of the sks file would register I had to enter "Crag_Pig" in the "Module" for all of the items in the sks file.

Interestingly, on any projects that didn't have spaces or special characters I didn't have to enter any thing for Module

like image 28
Ron Myschuk Avatar answered Nov 18 '22 05:11

Ron Myschuk


Thanks. Even the default template in Xcode for SpriteKit Games has this very issue. That since the release of swift 4 + iOS 11. I always thought it was me somehow, but never took the time to find why. Until now out of curiosity. I changed the line you wrote/fixed and voila.

Just changed the capitals in the parameter fileNamed: of your line

if let scene = GameScene(fileNamed: "GameScene")
like image 3
overNaut Avatar answered Nov 18 '22 05:11

overNaut