Reputation: 11
I am a beginner in swift, I have made a simple game and I wanted to add a menu scene. This is what I have so far.
In menueScene.swift
import Foundation
import spritekit
class MenuScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let Label = SKLabelNode(fontNamed:"Chalkduster")
Label.text = "Choose the Device";
Label.fontSize = 25;
Label.position = CGPoint(x:CGRectGetMidX(self.frame), y:665.523254394531);
self.addChild(Label)
let Label1 = SKLabelNode(fontNamed: "Chalkduster")
Label1.text = "light's";
Label1.fontSize = 20;
Label1.position = CGPointMake(401.463256835938,545.199401855469)
self.addChild(Label1)
}
}
and in GameViewController.swift
import UIKit
import SpriteKit
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)
}
}
And this displays nothing in the iOS simulator and no errors. What am I doing wrong.
Upvotes: 1
Views: 1625
Reputation: 3182
I think it will be much easier for you to use visual tools. You can create new scene using File -> New file -> SprikeKitScene.
In this case you will have "MenuScene.sks" file. You can drag and drop elements to this scene (labels, sprites etc). All your logic you will put into "ManuScene.swift".
Code in your main ViewController to show your menu on a startup:
class YourViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = MainMenuScene(fileNamed:"MenuScene") {
let skView = self.view as! SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFit
skView.presentScene(scene)
}
}
...
}
Later if you need to open your menu again from another scene, simply call this code:
let nextScene = GameScene(fileNamed: "ManuScene")!
nextScene.scaleMode = .AspectFit
scene?.view?.presentScene(nextScene)
Upvotes: 1
Reputation: 190
Seems like the CGPoints you want for the Label.position are placed out of the view. This is because the x and y values are to big. I don't know why your values are this big, I assume you where working with an iPad as simulator device. Try replacing it with this:
override func didMoveToView(view: SKView) {
let Label = SKLabelNode(fontNamed:"Chalkduster")
Label.text = "Choose the Device";
Label.fontSize = 25;
Label.position = CGPoint(x:CGRectGetMidX(self.frame), y:20.0);
self.addChild(Label)
let Label1 = SKLabelNode(fontNamed: "Chalkduster")
Label1.text = "light's";
Label1.fontSize = 20;
Label1.position = CGPointMake(CGRectGetMidX(self.frame) ,300.0)
self.addChild(Label1)
}
Upvotes: 0