viewWillLayoutSubviews in Swift - swift

I'm trying to translate SKScene * scene = [GameScene sceneWithSize:skView.bounds.size]; into swift but I get the error
'sceneWithSize' is unavailable: use object construction 'SKScene(size:)'.
I am using viewWillLayoutSubviews and cutting out the viewDidLoad() because it does not give correct dimensions for the screen dimensions of the device I choose. It actually makes me question why viewDidLoad() exists at all?
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews();
let skView = self.view as SKView;
if skView.scene != nil {
skView.showsFPS = true;
skView.showsNodeCount = true;
skView.showsPhysics = true;
// Create and configure the scene
let scene:SKScene = GameScene.sceneWithSize(skView.bounds.size); // ERROR MESSAGE!
// Objective-C code in next 2 lines
// SKScene * scene = [GameScene sceneWithSize:skView.bounds.size];
// scene.scaleMode = SKSceneScaleModeAspectFill;
// Present Scene
skView.presentScene(scene)
}
}

Try changing
let scene:SKScene = GameScene.sceneWithSize(skView.bounds.size);
by
let scene:SKScene = GameScene(size: skView.bounds.size);
Hope this helps ;)

As the error says, the function you are trying to use is not available. You should instead use the constructor init(size size: CGSize):
let scene = SKScene(size: skView.bounds.size)
Note also that the :SKScene is not necessary as the type is obvious from the constructor.
If you open the documentation, you will see that +sceneWithSize: is not available for Swift.

When a view's bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.

Related

How to change SKScene position before presenting?

I am trying to adjust SKScene position based on the device the game is running. For example due to 'top notch' area of iPhone XS Max, I need to subtract that height from SKScene height and position the scene accordingly.
So here is how I calculate the height aka safeAreaHeight in the AppDelegate and subtract from scene height aka sceneHeight:
safeAreaHeight = UIApplication.shared.statusBarFrame.size.height
sceneHeight -= safeAreaHeight
In GameViewController I try to re-position the SKScene like below:
if let view = self.view as! SKView? {
// Load the SKScene from 'MainMenuScene.sks'
if let scene = SKScene(fileNamed: "MainMenuScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFit
scene.size.height = sceneHeight
scene.size.width = sceneWidth
scene.position.y -= safeAreaHeight
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
However, I am getting below error:
SKScene: Setting the position of a SKScene has no effect.
Additionally, I tried to use SKAction to move position of the SKScene as well, then I received below error:
SKScene: Animating the position of a SKScene has no effect.
Thank you for the help.
Well I figured how to achieve this, I am sharing in case it would help someone else.
Inside AppDelegate you get the correct safe area values and store it in a singleton (in my case: safeAreaHeight) for access throughout the app. Subtract the amount from scene height (sceneHeight).
let kWindow = UIApplication.shared.windows[0]
safeAreaHeight = kWindow.safeAreaInsets.top + kWindow.safeAreaInsets.bottom
screenHeight = screenRect.size.height - safeAreaHeight
Don't forget the adjust the sceneHeight in GameViewController before presenting it:
if let view = self.view as! SKView? {
// Load the SKScene from 'MainMenuScene.sks'
if let scene = SKScene(fileNamed: "MainMenuScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFit
scene.size.height = sceneHeight
scene.size.width = sceneWidth
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
Inside loaded scene, in this case it is MainMenuScene, you simply find subview of SKScene and adjust its Y position by subtracting the safeAreaHeight at its center
guard let subView = self.view else { return }
subView.center.y -= safeAreaHeight
PS.: Do not adjust all the views in different scenes (ie.: GameScene, GameOverScene etc.) Once you adjust, it remains throughout the app.

Changing the initial game scene in Swift

I want to change the initial scene being presented to be another class other than the default GameScene class. From reading other questions, I understand I must change this part from the GameViewController:
if let scene = SKScene(fileNamed: "GameScene") {
print(scene)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
So within the GameScene.swift file I am creating a new class:
class MainMenu : SKScene {
override func didMove(to view: SKView) {
print("At least it ran")
self.scene?.view?.presentScene(GameScene())
}
}
However, when I change the scene to:
if let scene = SKScene(fileNamed: "MainMenu")
When I run the project, it gets stuck, but when I run it with the string "GameScene" then it works perfectly. I am doing something wrong loading the MainMenu?
In my opinion you should separate your scenes into their own files...
Do you have an corresponding SKS file for MenuScene? You need to create one if you are trying to load it with fileNamed:
or -
Use this code to load a SKScene file that is created in code only and not in the Scene editor
if let skView = self.view as? SKView {
if skView.scene == nil {
let scene = MenuScene(size: skView.bounds.size)
scene.scaleMode = .aspectFill
skView.presentScene(scene)
}
}
and then in your MenuScene file you will need an init func
init(size: CGSize) {
super.init(size: size)
name = "menuScene"
}

GameScene misses initializers

I get an error that "Class 'GameScene' has no initializers" when I try to add a level selection to my game.
I present the scene in GameViewController.swift like this:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.level(1) {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = 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
skView.presentScene(scene)
}
}
And in my GameScene.swift I wanted to handle the level selection like this (got this from Ray Wenderlich iOS Game Development Books)
class func level(levelNum: Int) -> GameScene? {
let scene = GameScene(fileNamed: "Level\(levelNum)")!
scene.currentLevel = levelNum
scene.scaleMode = .AspectFill
return scene
}
But then I get the error as I mentioned and I dont know how to fix this issue :-? Are there any other ways to make a level selection? Currently I have for each level one sks file, which is something I find really handy, since I enables me to lay out the levels visually.
Any recommendations and/or help?
Thanks in advance

How to get and change a variable in my view controller from SKScene

Ok so here is the code
class GameViewController: UIViewController, SceneTransitionDelegate,
GKGameCenterControllerDelegate, ADBannerViewDelegate {
var coolbool:Bool = false
...abunch of unimportant stuff functions and stuff
}
And here is what I am trying to do from my SKScene
func thing1()
{
let controller = GameViewController()
controller.coolbool = true
println(controller.coolbool) // Will say that it is true
sceneDelegate.transitionToScene(Menu.self) //Menu.self is the skscene that
we used to be in and will be in
}
func thing2()
{
println(controller.coolbool) // Will say that it is false
if (controller.coolbool == true)
{
//Put rainbows over every sprite and change generator settings
}
}
So basically what happens is that "coolbool" is initialized as being false. Until thing1() is called causing the variable "coolbool " to change. And i confirm its change immediately after, before the transition. However after the transition (to the same scene (I'm trying to make it look different if the bool is true)) if you ask what the value is, it says its false.... even though i just set it to true.
Anyway I assume I am doing something wrong, is their a better way to do this??? Incase you want it here is the transition function
func transitionToScene(sceneClass:Scene.Type) {
playing = false
var sizeRect = UIScreen.mainScreen().applicationFrame
var width = sizeRect.size.width * UIScreen.mainScreen().scale
var height = sizeRect.size.height * UIScreen.mainScreen().scale
let skView = self.view as! SKView
let scene = sceneClass(size: skView.bounds.size)
scene.size = CGSizeMake(width, height)
rwidth = width
rheight = height
swidth = width
sheight = height
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
scene.sceneDelegate = self
skView.presentScene(scene)
}
You need to get the current instance of the GameViewController, so this statement:
let controller = GameViewController()
isn't going to work because it declares a new instance of a controller, which is not related to the SKView you are currently using. Instead, we need to obtain the current, presenting view controller.
Obtaining Instance of the View Controller:
The closest method that can help us here is in the UIResponder class, and it is nextResponder:. Its general function is to find and return the current next responder, which seems vague and not of much use to us, until Apple mentions that:
Subclasses must override this method to set the next responder. UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)
This means that if we are in a scene, we can find the view controller by using the scene's self.view which is the presenting SKView object (inherits from UIView). All we have to do is find the View Controller in our responder chain.
Swift:
var currentViewController: GameViewController?
var upstreamResponder: UIResponder? = self.view
var found = false
while (found != true){
upstreamResponder = upstreamResponder!.nextResponder()
if let viewController = upstreamResponder as? GameViewController {
currentViewController = viewController
found = true
}
if upstreamResponder == nil {
//could not find VC, PANIC!
break
}
}
Objective-C: (will probably have to #include "GameViewController.h")
GameViewController *currentViewController;
UIResponder *upstreamResponder = self.view;
BOOL found = false;
while (found != true) {
upstreamResponder = [upstreamResponder nextResponder];
if ([upstreamResponder isKindOfClass:[GameViewController class]]) {
currentViewController = (GameViewController *)upstreamResponder;
}
if (upstreamResponder == nil) {
//omg where did VC go?!
break;
}
}
Setting properties on your View Controller
Now that you have your current view controller, it is very simple to set any desired property. So if you wanted to change coolbool, all you have to do is state:
currentViewController.coolbool = true
Hope this helps! -Max

Scene size in Xcode 6 / SpriteKit / Swift

I've been getting stuck doing what should be really simple stuff in Xcode, I am trying to add a sprite to the scene and for it to sit in the bottom left corner:
var groundTexture : SKTexture = SKTexture(imageNamed: "dirt-block")
var ground : SKSpriteNode = SKSpriteNode(texture: groundTexture)
ground.position = CGPointMake(ground.size.width/2, ground.size.height/2)
self.addChild(ground)
This doesn't show anything, so I took a look at what the scene dimensions are and self.frame.size.width and self.frame.size.height are returning a width of 1024 and a height of 768, regardless of orientation.
I want this fixed in landscape mode if that makes any difference to the answer?
I'm obviously missing a very fundamental idea to do with setting up the scene, if anybody could shed any light it would be much appreciated.
Thanks.
You have to edit the GameViewController:
Cut everything from viewDidLoad() except super.viewDidLoad()
Overwrite viewWillLayoutSubviews
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
var 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.size = skView.bounds.size
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
Also you should set the scene size as you can see
scene.size = skView.bounds.size
Hope this helps
There is also a great Tutorial where this is explained better:
http://www.raywenderlich.com/49721/how-to-create-a-breakout-game-using-spritekit
(Chapter 1 or 2)