SKScene not loaded when add GKComponent to GameScene.sks - ios

I am currently creating a 2D game with Swift SpriteKit and GameplayKit.
I have a GameScene.sks file and a GameScene.swift file that is of type SKScene.
Now I wanted to add a Component of type GKComponent. I created my component and added it to one of my SKSpriteNodes in GameScene.sks
However after adding the Component to the SpriteNode in the Scene Editors Inspector the Scene is no longer loaded and the app shows only a grey screen.
Is this a known issue in XCode?
Is there a way of bypassing this issue?
I have not uploaded any example code. The issue is reproducable by any new XCode Project (ios / Game) that supports SpriteKit and GameplayKit.
Just add a class that is derived from GKComponent and add the class to the given helloLabel in the Scene Editor.
This is how the GameScene is loaded:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GKScene(fileNamed: "GameScene") {
if let sceneNode = scene.rootNode as! GameScene? {
// This gets only executed when I remove the Component from the SKSpriteNode in the Scene Editor.
}
}
...

Related

SpriteKit Scene referenced with SKReferenceNode does not load custom class

I have a GameScene.sks a custom class GameScene.swift which is connected via the Custom Class Inspector (on the right in Xcode). This is working fine.
Now in the GameScene.swift where I want to reference another scene that I want to reuse in many scenes in the future (oversimplified code):
class GameScene: SKScene {
// ...
override func didMove(to view: SKView) {
//...
guard let gameMenuPath = Bundle.main.path(
forResource: "GameMenu", ofType: "sks") else { return }
let url = NSURL(fileURLWithPath: gameMenuPath) as URL
let gameMenu = SKReferenceNode(url: url)
addChild(gameMenu)
//...
where I am loading a reusable GameMenu.sks.
Everything works fine till here. Now the custom class issue for the GameMenu.sks (the scene to be reused). For the GameMenu.sks I also have a GameMenu.swift which is referenced in the custom class inspector as custom class. So exactly as for the GameScene. The problem is that it seems like the GameMenu.swift is not found/loaded. I get no error - so it looks like I misspelled the custom class - but I checkt it many times and also cleared the Xcode cache.
Any ideas how to set custom class for a scene (.sks) that is referenced in another scene?
I would think this is something that is done for reusable scene parts suche as the GameMenu in my case that is visible in every GameScene.
Thank you for any hints.
Is it necessary to add GameMenu in GameScene via code? You can also simply use the inspector. In your GameScene.sks drop the 'Reference' (SKReferenceNode) object. Select it from the hierarchy and select GameMenu as in the 'reference' dropdown.
SKReferenceNodes behave as nodes, not as scenes, which may cause confusion since you are creating a scene file.
This means none of the functions that a view calls on a scene will be called.
If you need some of these functions to call, you need to do it manually.
Look at this example to get your didMoveToView to fire:
override func didMove(to view: SKView) {
let gameMenu = SKReferenceNode(fileNamed:"GameMenu") as? SKScene // (or your custom class)
addChild(gameMenu)
gameMenu.didMove(to:view)
}

Nothing Shows up on scene in SpriteKit

I wanted to create a game in Xcode 7.2. I wanted to create a info screen which will have some labels inside. But nothing shows up on the newly created scene. Whatever I put on the new scene, like images, sprites, labels. Nothing appears on the screen when I run it. But the code I wrote to create the label works perfectly fine in the "GameScene" which the game initially contains.
The code that I put in the InfoScene is the following:
import SpriteKit
class InfoScene: SKScene {
override func didMoveToView(view: SKView) {
let thanksLabel = SKLabelNode(fontNamed:"Arial")
thanksLabel.text = "Thank you for Playing!"
thanksLabel.fontSize = 45
thanksLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(thanksLabel)
}
}
Nothing seems wrong to me. If I put the above code in the GameScene which the template originally has, them the label will appear. Also, if I add a sprite on the scene, it doesn't show up as well. This really confuses me.
I think you've simply forgot to put your code to an instance method like:
Swift 2.x:
override func didMoveToView(view: SKView) {
// put your stuff here
}
Swift 3.x:
override func didMove(to view: SKView){
// put your stuff here
}
didMove is called immediately after a scene is presented by a view. According to the actual documentation you might use this method to create the scene’s contents.
Update: (after your corrections and comment):
I think InfoScene is not launched for some reason.
Make sure your InfoScene is launched using breakpoints inside didMoveToView or simply add this line between your lines:
print("∙ \(type(of: self))")
Are you sure you are using your InfoScene class? How are you making this instance? Through an SKS file? if so, you need to change the Custom class field to point to your InfoScene, by default it will go to SKScene

How can you connect a swift file with an SKscene?

I looked through all posts, WWDC talks and youtube videos, but I still can't figure out how to have multiple SKscenes for different levels and connecting them with swift files in a Game using Spritekit. I want to have a main level board and I managed to open a new scene if you press a SKSpriteNode, but how can I implement game logic to this specific SKScene?
Apologies in advance if it is a silly question, I've been spending ages on it.
You can do it like this:
1) Create .swift file and make a subclass of a SKScene
Go to :
File menu -> New File -> Source -> Swift file
and make subclass of a SKScene
class MenuScene:SKScene {}
2) Create .sks file
Then go to
File menu -> New File -> Resource -> SpriteKit Scene
and make a MenuScene.sks (name it MenuScene without the actual extension).
Repeat this steps for each scene you want to have.
Then to load and start your initial scene. Do this inside your GameViewController.swift:
if let scene = GameScene(fileNamed:"GameScene") {
let skView = self.view as! SKView
//setup your scene here
skView.presentScene(scene)
}
To make a transition to other scene (lets assume that you are in the MenuScene currently) you should do something like this:
if let nextScene = GameScene(fileNamed: "GameScene"){
nextScene.scaleMode = self.scaleMode
let transition = SKTransition.fadeWithDuration(1)
view?.presentScene(nextScene, transition: transition)
}

How to create more SKScene in addition to GameScene

I tried to create a new subclass of SKScene "MainScene" like the one apple created the GameScene.
I want to create more scene in addition to my "GameScene" but its not working.
Below is my subclass code.
MainScene :
import SpriteKit
#if !os(iOS)
import AppKit
#endif
class MainScene : SKScene {
override func didMoveToView(view: SKView) {
backgroundColor = UIColor.blueColor()
}
}
MainSceneViewController :
import UIKit
import SpriteKit
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = MainScene(fileNamed:"MainScene") {
// 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
skView.showsPhysics = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
Error : "Could not cast value of type 'UIView' (0x1097f2b20) to 'SKView' (0x108a4cad0)."
When creating a new scene, you need to actually create a new scene file, not just the code behind file.
To create new scene files, go to file, New, File, then in your sections go to iOS, SpriteKit Scene. This will create the sks file you need.
edit:
In the case you are getting the error Could not cast value of type 'UIView' to 'SKView', this is due to the fact that in your story board, your ViewControllers main view does not have a custom class of SKView. In your storyboard file, open up your view controller, click the view for this controller, make sure the right panel is visible, and select the identity inspector tab (It is probably the 3rd from left, looks like a flag or an envelope with a window). Under Custom Class, look for the class text box, and put in SKView
To create more SKScene in addition to GameScene
Go to New File
Select Coca Touch Class
Click on Next
SubClass : Type Manually SkScene
create
import SpriteKit

Adding a screen before GameScene.swift in SpriteKit

I'm making a game in XCode using Swift and I want to add a starting screen before the actual game. I don't want just an image that fades out, I want a screen with "play", "select character", and some buttons. I don't know if i should add a new swift file, add a sks file, edit the GameScene.swift file? I'm just starting to develop in SpriteKit and it would be great if you could explain me how to do this properly. Thanks.
Follow this Step by step and it should work:
Open up your Interface Builder (main.storyboard) and add a UIViewController.
Select the new UIViewController, go to Editor > Embed In > UINavigationController
a. To remove the UINavBar for your game scene you'll be able to do this using the property navigationBarHidden in the Game Scene View Controller
Add the UIButtons and/or other controls of your choice to your new View Controller
Left Click (ctrl) and drag the PLAY UIControl to the View Controller displaying your Game Scene.
Connect the other UIControls after adding other View Controllers that are associated with their UIControl counterpart. (see below)
If you've only ever programmed using the SpriteKit Template file, I would recommend looking over this Apple Documentation
~ MORE Details ~
Having successfully added your Start Screen, or your Root View Controller, add one ViewController for each page, or screen, that you'll need the Start Screen to display, by dragging one at a time into the Storyboard file.
Having all the Screens you'll need represented by the View Controllers within your main.storyboard file, the next step is Control Clicking from each UIButton on your "Start Screen" to the ViewController that will display its target screen.
You can just create new sprite kit scene and new swift file,
then put class of your swift file into Custom Class inspector in the
gamescene.sks so it will work just like default GameScene.
then initialise your menu scene in viewDidLoad instead of original GameScene
if let menu = SKScene(fileNamed: "MainMenu") {
menu.scaleMode = .aspectFill
view.presentScene(menu)
}
You can switch to your original scene like this(for example):
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let posistion = touch.location(in: self)
let node = self.atPoint(posistion)
if node == yourNode {
if let originalScene = SKScene(fileNamed: "GameScene") {
originalScene.scaleMode = .aspectFill
view?.presentScene(originalScene)
}
}
}
}

Resources