I'm using spritekit with swift, and I'm just trying to present a new scene...but something is wrong and throwing errors. I was pretty sure this was the right syntax, and this is really throwing me for a loop.
The line
let skView = self.view as SKView
is giving me the error "SKView? is not convertible to SKView
Any advice is appreciated!
*my current code is below:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches{
let location = touch.locationInNode(self)
if(self.nodeAtPoint(location) == self.playButton)
{
var scene = PlayScene(size: self.size)
let skView = self.view as SKView
// Configure the view.
/* 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)
}
}
}
The problem is the view property on SKScene is an optional (SKView?) because an SKScene doesn't necessarily have a containing view; what's it contained in before its been presented? Nothing.
To solve your problem you need to check the scene has a view by unwrapping the view property, by using optional binding for example:
if let view = self.view {
let scene = PlayScene(size: self.size)
scene.scaleMode = .AspectFill
view.presentScene(scene)
}
You can be pretty certain your SKScene has been presented (and thus view isn't nil) if someone is pressing on it - therefore you could instead force unwrap view, like so:
let scene = PlayScene(size: self.size)
scene.scaleMode = .AspectFill
view!.presentScene(scene)
Also, it's not required that you configure the SKView again (skView.ignoresSiblingOrder = true) since this, presumably, was already done in GameViewController.
Edit:
The line let skView = self.view as! SKView is likely from GameViewController (a subclass of UIViewController). If you take a look at the documentation for UIViewController and SKScene you'll see they both have a view property:
class UIViewController: /* Superclass and Protocols */ {
var view: UIView!
// ...
}
class SKScene: SKEffectNode {
weak var view: SKView?
// ...
}
However their types differ which means the way you use them differs. In the case of SKScene see above about unwrapping. In the case of UIViewController, you need to cast to SKView with the line:
let skView = self.view as! SKView
because UIView doesn't have the methods necessary for presenting an SKScene. (For more information on casting I'd recommending you take a look at The Swift Programming Language: Type Casting)
Normally you wouldn't be allowed to cast from UIView to SKView (keep in mind that SKView is a subclass of UIView). Try the following in Playgrounds:
let view = UIView()
let skView = view as! SKView
You should get an error something along the lines of:
Could not cast value of type 'UIView' (0x1041d0eb0) to 'SKView'
(0x10da10718)
However you can cast from UIView to SKView in your GameViewController because the Custom Class of GameViewController's view has been set to SKView:
I hope that helps clear up any confusion you had.
try this :
let sKView = self.view?.scene?.view
sKView?.presentScene(scene)
Best regards,
Yassine
Related
So I writing a program in swift, and am having trouble understanding the proper communication between GameScene and GameViewController:
Basically I have 'savefiles' which are plists, when loaded using a function "self.loadFromPlist(fileName: )" are loaded by clearing the previous array of nodes, and drawings a set of SKShapeNodes based on attributes from the dictionary elements of the loaded Plist
I needed a file browser, which I've implemented from a Cocoapod (FileBrowser), that must be called in the "GameViewController"
My Problem is this:
When calling my 'loadFromPlist' function from inside 'viewDidLoad' like this (to initialize the project) - the function works and the gamescene is drawn:
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)
let gameScene = scene as! GameScene
gameScene.loadFromPlist(scoreFileName: gameScene.defaultScore)
gameScene.gameViewControllerDelegate = self
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
When I try to do same thing in my own function in the GameViewController (called view a GameviewDelete in GamScene), the code executes the line and I can follow it with the debugger into the 'loadFromPlist' function in the gamescene, it executes the for loop drawing shapes etc, but it just does nothing.
func callloadplist() {
var loadedfilename2:String!
let file = FileBrowser()
present(file, animated: true, completion: nil)
if let scene = GameScene(size: view.frame.size) as? GameScene {
var strippedfilename: String?
file.didSelectFile = { (file: FBFile) -> Void in
let loadedfilename = file.displayName
scene.filebrowserselectedfilename = loadedfilename
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let myskscene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
let gameScene = myskscene as! GameScene
let strippedfilename = String(loadedfilename.dropLast(6))
print(strippedfilename)
gameScene.loadFromPlist(scoreFileName: "output")
}
}
}
}
}
So the function call working inside of 'viewDidLoad()' and also as 'self.loadFromPlist(fileName: )' in GameScene.swift, but cannot be called from a custom method/ function I've made in GameViewControllers -- Any Insights?
Thanks
So file.didSelectFile is setting up a closure to respond to the event of the file being selected. This was then running the function called on a different instance of a 'GameScene' Object.
I was able to refine my question and answer it here:
Swift - SKSprite Kit - Change Gamescene Variables from FileBrowser Closure
I want to write the iOS Game Example that uses SpritKit in Swift, which is provided with Xcode only in code. That means I don’t want to use the GameScene.sks, actions.sks and the main.storyboard. I know how to write it without storyboard, but I can’t get it working without the .sks files. Can you say what I must change or can you provide me with a full project?
You will first need to have a View Controller. You can adjust the properties how you would like them. Here is mine:
import UIKit
import SpriteKit
class GameViewController: UIViewController {
// MARK: View Controller overrides
override func viewDidLoad() {
super.viewDidLoad()
view = SKView(frame: view.bounds)
if let view = self.view as! SKView? {
// Initialise the scene
let scene = GameScene(size: view.bounds.size) // <-- IMPORTANT: Initialise your first scene (as you have no .sks)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
// Scene properties
view.showsPhysics = false
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
Then, you create a class for your first scene. Mine is called GameScene which was initialised in the view controller. Make sure this is a subclass of SKScene. It will look something like this:
import SpriteKit
class GameScene: SKScene {
/* All Scene logic (which you could extend to multiple files) */
}
If you have any questions, let me know :)
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.
Incorrect argument label in call have "size" expected "coder "
Any help ?
lass GameViewController: UIViewController {
var scene: GameScene!
override func viewDidLoad() {
super.viewDidLoad()
// Configure the view
let skView = view as! SKView
skView.isMultipleTouchEnabled = false
// Create and configure the scene
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .aspectFill
// Present the scene
skView.presentScene(scene)
}
If you override all of the superclass designated initializers, you'll
inherit the superclass convenience initializers. (See Swift docs,
"Initialization" -> "Automatic Initializer Inheritance" -> "Rule 2".)
I think in this case that means you can define an init(property1:)
that just calls through to super and you should able to use
init(value1:value2) for subclass initialization.
https://forums.developer.apple.com/thread/30537
Im following a tutorial right now and Im getting an error that I do not know how to fix.
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = StartGameScene(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 prefersStatusBarHidden() -> Bool {
return true
}
}
On line 8 the let scene = StartGameScene(size: view.bounds.size)
is giving me an error:
use of Unresolved Identifier ' StartGameScene'.
I have created a CocoaTouch class and named StartGameScene. I dont know what the issue is. How do I fix it?
StartGameScene would need to be a class declared like this:
class StartGameScene: SKScene {...}
Either StartGameScene is not declared as an SKScene or the class itself is not declared.
The cocoa touch class that you have created has to inherit from SKScene. SKScene is the class in which you define your game's scene. Hence your code should look like:
class StartGameScene: SKScene {
}
If you have already done this, then please make sure there isn't any typographic error.
just change StartGameScene to GameScene