i have the problem, that the GameViewController doesn't load my GameMenu. If i simulate my app it only shows a grey screen with node:0 and the fps count.
Here the Code from GameViewController.swift:
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 = GameMenu(fileNamed: "GameMenu") {
// 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
}
}
I tried also the line:
if let scene = SKScene(fileNamed: "GameMenu")
but it changed nothing.
I also added the Custom Class "GameMenu" to the GameMenu.sks
I think the problem is this if let scene = SKScene(fileNamed: "GameMenu") line.
I have read a lot about this problem but i my case nothing was successful.
I also tried this:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let gkScene = GKScene(fileNamed: "GameMenu.sks") {
if let skScene = gkScene.rootNode as? SKScene {
// Set the scale mode to scale to fit the window
skScene.scaleMode = .aspectFill
// Present the scene
view.presentScene(skScene)
}
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
but nothing happend.
You have to load it as GKScene from .sks file and get SKScene from its rootNode attribute.
if let gkScene = GKScene(fileNamed: "GameMenu") {
if let skScene = gkScene.rootNode as? SKScene {
// TODO
}
}
Related
Since I am new to swift and spriteKit, I have read the document from apple:
preload document from Apple
I tried to load some images like this code below:
let textureArray = [SKTexture(imageNamed: "enemy1"),SKTexture(imageNamed: "enemy2"),SKTexture(imageNamed: "enemy3")]
override func viewDidLoad() {
super.viewDidLoad()
SKTexture.preload(textureArray) {
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 = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
Now, my question is how to use the textures I have loaded? For example, in another swift file, I have a class called player, how could I use the textures I have loaded to crate a SkspriteNode? How to pass the textureArray to that class?
I am trying to direct the user to a main menu file as soon my game loads (made with Swift 4 SpriteKit). However, when I try to redirect the user to this .swift file (where a label should be displayed onto the users screen) the screen is simply blank except the for the node and FPS counter.
Here's my code in the MainMenuScene.swift file I created:
import Foundation
import SpriteKit
class MainMenu: SKScene {
override func didMove(to view: SKView) {
print("In scene")
let myLabel = SKLabelNode(fontNamed: "The Bold Font")
myLabel.fontColor = SKColor.blue
myLabel.text = "My Label"
myLabel.fontSize = 50
myLabel.position = CGPoint(x: 0, y: 0)
myLabel.zPosition = 1
self.addChild(myLabel)
}
}
Here's my code in the GameViewController.swift
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: "MainMenuScene") {
// 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
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
}
}
I changed the
if let scene = SKScene(fileNamed: "GameScene")
to
if let scene = SKScene(fileNamed: "MainMenuScene")
That line of code should redirect the user to MainMenuScene.swift, but when I run the program the screen is simply blank.
Anyone know what im doing wrong?
Thanks!
Try using the code below after
super.viewDidLoad()
Also make sure that your Menu class is called "MainMenuScene" and that you changed the scene name to the same at MainMenuScene.sks
CODE:
if let scene = GKScene(fileNamed: "MainMenuScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! MainMenuScene? {
scene.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
//view.showsFPS = true
//view.showsNodeCount = true
}
}
}
Tried to perform an SKTransition for the very first loading of GameScene from GameViewController but nothing seemed to happen. Can't we perform some transition while presenting a scene directly from the GameViewController? It seemed to me quite hilarious that the normal present scene is working but not the one with transition. Here is the code :
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
let scene = GameScene(size: view.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
//transition
let transition = SKTransition.doorsOpenVertical(withDuration: 2)
// Present the scene
view.presentScene(scene, transition: transition)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
self.backgroundColor = SKColor.red
}
}
Please solve the query.
Similar to this question, I'm trying to change the initial scene of my game to FirstScene.swift.
I changed the following in GameViewController.swift:
from:
override func viewDidLoad() {
super.viewDidLoad()
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 = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
to:
if let scene = SKScene(fileNamed: "FirstScreen")
FirstScreen.swift starts with
import SpriteKit
class FirstScreen: SKScene, SKPhysicsContactDelegate {
Also tried :
if let scene = FirstScreen(fileNamed: "FirstScreen")
I've also tried a new project and still no luck! Any ideas please?
Do it this way:
let scene = FirstScene()
scene.scaleMode = .aspectFill
view.presentScene(scene)
Note that the SKScene(fileNamed: String) constructor is used to unarchive a scene from a .sks file.
I am assuming you do not have a FirstScene.sks file in your project.
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.
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)
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)
}