I have a simple game and when the time is up the game is over. I have a restart button which currently just reloads the GameScene but for some reason my Nodes appear halfway down and halfway to the left of the screen. I can still use the full space of the screen to play the game though.
The images below show the game while working, end game state and then after reloading the GameScene.
I'm not sure how to fix this or what code to provide.
This is my function for reloading the GameScene:
func goToGameScene(){
let gameScene:GameScene = GameScene(size: self.view!.bounds.size)
let transition = SKTransition.fade(withDuration: 1.0)
gameScene.scaleMode = SKSceneScaleMode.fill
self.view!.presentScene(gameScene, transition: transition)
}
Replace:
gameScene.scaleMode = SKSceneScaleMode.fill
With:
gameScene.scaleMode = .aspectFit
Or:
gameScene.scaleMode = .aspectFill
You can also just completely copy the GameViewController code so you get something like this:
if let view = self.view {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFit
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
Just put that code where you want to reload the scene.
Related
Right now I load my GameScene like this
if let scene = SKScene(fileNamed: "GameScene") {
if bool {
// Change size to CGSize(a, b)
} else {
// Change size to CGSize(x, y)
}
view.allowsTransparency = true
scene.backgroundColor = .clear
view.presentScene(scene)
}
However, depending on a certain bool I want to be able to change the initial size of the GameScene. I know this can be done using let scene = GameScene(size: size) but that means I can't use the .sks file for the GameScene which I have most stuff on.
How can I variably change the initial size of the GameScene while still using the .sks file?
P.S. scene.size = CGSize(a, b) does not seem to change anything
I'm making a game using spritekit. I'm using a joystick in my game which is declared in a separate SKNode class called 'joystick'.
In this class, I add a UIGesturerecongiser to the view from the class. In the constructor method, one of the parameters is a SKView which is passed from the game scene.
Constructor Method in Joystick class:
init(colour: UIColor, position: CGPoint, skView: SKView) {
//constructor method
self.colour = colour;
self.parentposition = position;
self.view = skView;
super.init();
//setup properties
//user interaction is needed to allow touches to be detected
self.isUserInteractionEnabled = true;
//setup
setup();
}
In the games scene, I initialise the class like this:
class GameScene: SKScene {
func setupJoyStick() {
let joystick1 = Joystick(colour: UIColor.red, position: CGPoint(x: screenwidth / 3, y: screenwidth / 10 * 1.5), skView: self.view!)
self.addChild(joystick1)
}
}
Error:
When I run my app, I get an error because the 'self.view' return nil and because it is forced to unwrap, it causes a fatal error.
Where View is defined:
if let scene = GKScene(fileNamed: "GameScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
// Copy gameplay related content over to the scene
sceneNode.entities = scene.entities
sceneNode.graphs = scene.graphs
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill
sceneNode.size = view.bounds.size
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
Additional Info:
I'm using:
Xcode 9.2
Swift 4
Sprite Kit
I'm testing it on:
Iphone 6s
Latest version of IOS (non-beta), latest public release.
Can someone please explain why this is happening and how to fix this problem.
Thanks in advance, any help would be appreciated.
Looks like you're trying to get view property of SKScene but it's nil. It's because you didn't presented SKScene.
Unfortunately I haven't worked with SpriteKit but you can find info about view property here and about SKScene here.
I have 2 SKScene.
GameScene
WelcomeScene.swift
I wanted my screen to load up "WelcomeScene" first instead of the default GameScene.Thus, I've edited "GameViewController.swift" accordingly. However, all I get is a blank screen, and couldn't locate what went wrong.
Here's my code in "GameViewController".
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "WelcomeScene") {
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
If you have a corresponding SKS file created in the Scene editor then you load your scene like so...
// Load the SKScene from WelcomeScene.sks'
if let welcomeScene = WelcomeScene(fileNamed: "WelcomeScene") {
welcomeScene.scaleMode = .aspectFill
view.presentScene(welcomeScene)
}
if you do not have a corresponding SKS file created in the scene editor but would rather load the scene that was created in code use...
welcomeScene= WelcomeScene(size: skView.bounds.size)
welcomeScene.scaleMode = .aspectFill
skView.presentScene(welcomeScene)
The relevant code is copied below and I also put a simple test project on Github to demonstrate this situation:
https://github.com/prinomen/viewPresentSceneTest
I have a GameViewController and a GameScene class. I try to set the scene with SKScene(fileNamed: "GameScene") but that call is not working because the scene does not appear and the print statement after that line is not being called.
I know I could use a different way to set the scene, like this:
let scene = GameScene()
But I'm trying to understand SpriteKit and it bothers me that the code below does not work. In another project I was able to successfully set the scene using SKScene(fileNamed: "GameScene") like in the code below.
Does anyone know why it is not working in this project?
GameViewController.swift
import SpriteKit
class GameViewController: UIViewController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
print("SKScene was set")
// Set the scale mode fit the window:
scene.scaleMode = .aspectFill
// Size our scene to fit the view exactly:
scene.size = view.bounds.size
// Show the new scene:
view.presentScene(scene)
}
}
}
}
GameScene.swift
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
let logoText = SKLabelNode(fontNamed: "AvenirNext-Heavy")
logoText.text = "Game Scene"
logoText.position = CGPoint(x: 0, y: 100)
logoText.fontSize = 60
self.addChild(logoText)
}
}
I believe you need an .sks file to load scenes like that. You probably deleted it from this project, but still kept it around in the other one.
Here's what the documentation says:
The name of the file, without a file extension. The file must be in
the app’s main bundle and have a .sks filename extension.
I'm new to Swift and have no Objective-c experience. I already have a working Swift game that I programed using SpriteKit and a not functional Menu also in SpriteKit. Im trying to figure out how to integrate this two but I can't find information on how to handle SKScene (I asume).
Currently if I Run the file it goes straight to the game and I want to first launch the menu and when a button is pressed to jump to the game. How or where can I learn to do this?
My two file clases are as follows:
class GameScene: SKScene, SKPhysicsContactDelegate {
// handles the game
}
class Menu: SKScene {
//handles the menu
}
thank you
You should have ViewController.swift after you make standart SpriteKit application. Your game scene is created and displayed in this file. To first show the menu, it is necessary to replace the game scene creating to menu scene creating. And your game scene must be created on menu scene object. It may look something like this:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = Menu(size: CGSize(width: 1024, height: 768))
let skView = self.view as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
....
}
class Menu: SKScene {
...
func startPlayButtonPressEvent() {
let skView = self.view as SKView
skView.ignoresSiblingOrder = true
let scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
...
}