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
Related
I want to learn SpriteKit and am following this tutorial, https://www.raywenderlich.com/187645/spritekit-tutorial-for-beginners-2
I have copied and pasted the same code from the tutorial into GameScene.swift as well as moved the picture of the ninja into Assets.xcassets
However, when I get two errors,
Before I run it I get
Forced cast of SkView to same type has no effect
After I run it I also get
Thread 1: EXC_BAD_ACCESS(code=2, address=0x7ffeed594fa8
When I run it, the app is simply blank... The app should show FPS count and NodeCount?
Here's my code:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .resizeFill
skView.presentScene(scene)
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
All of this code is in GameScene.swift
If someone could please help me that would be great!
UPDATE: I am using Swift 4
It seems you have an infinite recursion there. Note that you already have a GameScene instance but in the didMove you are creating another GameScene which you are presenting again which again is presented in the view and didMove is called again and so on. I think your code should look something like this:
override func didMove(to view: SKView) {
self.size = view.bounds.size
view.showsFPS = true
view.showsNodeCount = true
view.ignoresSiblingOrder = true
self.scaleMode = .resizeFill
}
I have five iPad scenes, and 5 iPhone Scenes in my GVC I have this, I can call the Welcome Scene and Hud Scene no problem, yet I can not replicate the same calls for the other scenes. I do not know if it is the curly braces, yet I can not seem to call the other iPad scenes it defaults to the iPhone scene. (trust me I was pretty excited when the HUD, showed up.) Can someone give some advice on the structure of my code. Please....
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
if let scene = WelcomeScene(fileNamed:"WelcomeScene") {
let skView = self.view as! SKView
skView.showsFPS = GlobalData.Constant.DEV.DEBUG
skView.showsNodeCount = GlobalData.Constant.DEV.DEBUG
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
} else {
if let scene = WelcomeScenePad(fileNamed: "WelcomeScenePad") {
let skView = self.view as! SKView
skView.showsFPS = GlobalData.Constant.DEV.DEBUG
skView.showsNodeCount = GlobalData.Constant.DEV.DEBUG
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
if let scene = HudScenePad(fileNamed: "HudScenePad") {
let skView = self.view as! SKView
skView.showsNodeCount = GlobalData.Constant.DEV.DEBUG
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
} else if let scene = WelcomeScene(fileNamed:"WelcomeScene") {
let skView = self.view as! SKView
skView.showsFPS = GlobalData.Constant.DEV.DEBUG
skView.showsNodeCount = GlobalData.Constant.DEV.DEBUG
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
}
override func prefersStatusBarHidden() -> Bool {
return false
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
}
This bit of code lets me know that the game scene is going to proper sks file, and prints the value in the editor, to verify, this is in my gameOver Function.
if (UIDevice.currentDevice().userInterfaceIdiom == .Pad ){
if let _ = SceneTypePad(rawValue: fullSKSNameToLoad + String("Pad")) {
fullSKSNameToLoad = "fileNamed" + "Pad"
goToScenePad(SceneTypePad.DebateScene1Pad)
print(("Went to DebateScenePad"))
_ = SKTransition.fadeWithDuration(3)
_ = DebateScene1Pad()
}
} else if (UIDevice.currentDevice().userInterfaceIdiom == .Phone ){
goToScene(SceneType.DebateScene1)
(print("Went to DebateScenePhone"))
_ = SKTransition.fadeWithDuration(3)
_ = DebateScene1()
}
Three secs is too long to wait btw... :)
I needed to create a new static variable to call the SKs pad scene in my GlobalData/Base Scene file. Noob mistake. Word to the wise when duplicating code for the new files double check the variables, when duplicated swift files. Also, it is good to give the mind a break, individuals can not keep their attention span and be sharp after 14 hours of coding.... Take a walk, sort it out with a fresh mind. Deadlines are crucial in this business, yet sometimes pressure can bring about silly decisions that can be easily solved.
static var previousScenePad:SceneTypePad?
static var nextScenePad:SceneTypePad?
if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
if (UIDevice.currentDevice().userInterfaceIdiom == .Pad ){
if let _ = SceneTypePad(rawValue: fullSKSNameToLoad + String("Pad")) {
fullSKSNameToLoad = "fileNamed" + "Pad"
}
} else if (UIDevice.currentDevice().userInterfaceIdiom == .Phone ){
}
Does work in GVC when calling particular SKS files. Obviously it renders the proper view based upon using the correct fileName "" that is defined in the base class, yes I am aware, that there are better way to use one SKS files, and using camera nodes to adjust views. When it comes to content, and having more space it gives opportunity to create a different experience with more pixels, and more space. Assets are equally important, as in the beginning I was calling the wrong assets per device, I decided to hard code, the scale factors for my look without losing resolution. The end result will be a universal app that works cross platforms. If you optimize right, I was able to condense a 140mb app, to 76mb, even though I added 5 new scenes to my app bundle. Check your background music, check your sound effects, and PNG files. Limit emitters to the effect that looks good. You can always go back and find cleaner approach in the next update!!!
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.
I have some troubles to make the invite accept in swift.
could someone help me with the right coding? Here is mine
GKMatchmaker.sharedMatchmaker().matchForInvite(Invitation!, completionHandler = {(InvitedMatch:GKMatch!, error: NSError!) -> Void in
if InvitedMatch != nil {
myMatch=match
LocalGame=false
if let scene = GameScene.unarchiveFromFile(environment_Prefix!+"GameScene") as? GameScene {
// 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 = .Fill
skView.presentScene(scene, transition: SKTransition.flipVerticalWithDuration(2.0))
}
}
})
Thanks
Finally I have figured out the solution which works. I had to implement the GKLocalPlayerListener like this and call the match for invite within the delegate function.
func player(player: GKPlayer!, didAcceptInvite invite: GKInvite!) {
GKMatchmaker.sharedMatchmaker().matchForInvite (invite, {(InvitedMatch, error) in
if InvitedMatch != nil {
myMatch=InvitedMatch
LocalGame=false
if let scene = GameScene.unarchiveFromFile(environment_Prefix!+"GameScene") as? GameScene {
// 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 = .Fill
skView.presentScene(scene, transition: SKTransition.flipVerticalWithDuration(2.0))
}
}
})
}
To get the player function called I have to register the listener at the local player authenticated block like this:
localPlayer.registerListener(self)
Now game invite works perfectly.
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)