I have a swift game that uses SpriteKit. I made a game but now I want to create a menu scene. So I changed the GameViewController to load MenuScene.swift like so
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as MenuScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = MenuScene.unarchiveFromFile("MenuScene") as? MenuScene {
//if scene = MenuScene.unarchiveFromFile("MenuScene") as? MenuScene {
// 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
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}
It loads MenuScene just fine and I created a play button but now I want to transition to gamescene when clicked so I put in this code
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if(play.containsPoint(location)) {
gamescene = GameScene(size: self.scene!.size)
self.view!.presentScene(gamescene, transition:reveal)
}
}
}
I have tried numerous other attempts but each time it has crashed into AppDelegate.swift with the error: Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFLT)
So what am I doing wrong?
self.scene!.sizewithself.size- user4233369self.scene!.sizewithself.frame.size- Wiem