Swift SpriteKit "Extra Arguement "size" in call - swift

When trying to transition between a SKScene to another SKScene. For example when its game over I use the code below and get the following error:-
Extra Arguement "size" in call
Below is a sample of my code used when this error occurs.
let scene = GameOverScreen(size: self.scene!.size) //<<---- Error throws here
scene.scaleMode = SKSceneScaleMode.AspectFill
view!.scene?.paused = true
self.scene!.view!.presentScene(scene, transition: transition)
This has never happened before in previous games of mine, and I cant seem to figure out why?
Using XCode 7, swift 2 and SpriteKit
Thanks in advance,
Rachel

I'm guessing this is happening because you have other initializers defined in GameOverScene that you haven't mentioned. Since you do that, you don't automatically inherit all of SKScene's initializers. You probably just have to add the following to GameOverScene:
override init(size: CGSize) {
// Set up your properties
super.init(size: size)
// Do whatever else you need to
}
If you didn't have any initializers defined in GameOverScene, you wouldn't be seeing this behavior. I'd suggest doing some further reading on Automatic Initializer Inheritance. The section I've linked to on that page describes the rules for when a class inherits the initializers of a superclass. They are not inherited by default.

Related

Using properties of SKSpriteNode for subclass

this is more of a general question about subclasses and inheritance within Swift 4.
In Apple's developer guide, a subclass ("anySubclass") of a custom class ("anySuperclass") can directly access/inherit the parent's properties, e.g.:
anySubclass.anyProperty = xyz (anyProperty was only defined within the Superclass)
To my question:
I'm currently working on my first game using SpriteKit and wanted to create subclasses of SKSpriteNode, one of which should be clBackGround.
If I just define it as a subclass of SKSpriteNode, i.e.
class clBackGround:SKSpriteNode {
}
I would think that I could simply use all properties associated with SKSpriteNode just like this:
var bg1 = clBackGround()
bg1.size = ...
.
.
.
This does not seem to be working however. So my questions is:
Do I need to initialize all the properties of SKSpriteNode I want to use within clBackGround (using a designated initializer)? How can I then assign an image to an instance of this subclass?
Any help would be much appreciated!
Not sure if this is what you're asking, but, generally, if you create a subclass of an object, you don't need to do anything special to access the superclass's properties. They'll be initialized in the usual fashion.
However, you DO have to ensure that the superclass's initializers get called in a proper manner if you have your own initializers for your subclass.
In the case of a subclass of SKSpriteNode, if you define your own initializer for the subclass, that initializer will need to call the designated superclass initializer, which passes in the texture, color, and size to use for the sprite:
init(enemyType:String) {
let texture = SKTexture(imageNamed: "enemy_\(enemyType)")
let color = UIColor.black
let size = texture.size()
super.init(texture: texture, color: color, size: size)
}

Scene physicsbody not working when using init(size:) for my scene?

Instead of using the suggested initializer, init(fileNamed: String), I want to use the designated initializer, init(size: CGSize), in my SKScene class.
When I do this my scene is the correct size but the physicsBody = SKPhysicsBody(edgeLoopFrom: frame) does not work for my scene.
Does the init(fileNamed) initializer do anything that I'm missing?

Unable to pass a variable to a scene in spritekit in swift

I'm currently having trouble with passing variables to a scene in spritekit (swift). In android when moving to a different activity, I just set the parameter to the Intent and simply get the variable. But since I'm new to spritekit and swift, i find it difficult. Is there a clear way to pass a variable to the scene ? I have a sample code below that I tested but did not work.
class GameSKView: SKView {
var mylevel = 0
}
//Inside my main GameScen
let gameView = self.view as GameSKView
sample.text = "my level is :\(gameView.mylevel)"
Try this:
class GameSKView: SKView {
internal var mylevel = 0
}
And inside you main game scene
if let gameView = self.view as GameSKView {
sample.text = "my level is :\(gameView!.mylevel)"
}
Finally found the solution.
In order to use the custom SKView, I have done the following procedures.
Open the Idenity inspector in the Main.storyboard
Replace the [SKView] inside the Custom Class with your [customSKView]
, In my case GameSKView
Save. That was all I needed to do.

super.init() recursive issue in Swift

I am working on a SpriteKit game using Swift and I need to subclass SKSpriteNode. In the init() (which is not an override) function I initialize the subclass's properties then call the super:
init(selector: Selector, delegateScene: SKScene, text: String, position: CGPoint) {
self.delegateScene = delegateScene
self.labelNode = SKLabelNode()
self.labelNode.position = CGPointZero
self.labelNode.text = text
self.selector = selector
super.init()
}
I set a breakpoint at the super.init() and a EXC_BAD_INSTRUCTION exception occurs right after it at the beginning of the subclass's init. Here's the error:
(file path).swift: 12: 7: fatal error: use of unimplemented initializer 'init(texture:color:size:)' for class 'Energies.Button'
If you inherit from SKSpriteNode, you should call the designated initializer. In this case that needs to be init(texture:color:size:). So if you were using a image or texture, you would need to call super.init like this:
super.init(texture: texture, color: nil, size: CGSize(width: 100, height: 100))
Note that you'll also need to set a size when initializing.
But in this case you can forget the above, because you're not looking for a subclass of SKSpriteNode, but a subclass of SKNode. If you change the superclass in the declaration from SKSpriteNode to SKNode it should work immediately.
You'll also need to add your add your label to the node after super.init(), else your label wouldn't be shown and your node would be empty.

init var with swift using SpriteKit?

How to correctly init with swift using SpriteKit?
This compiles but I get a error once the simulator starts running. "swift_reportUnimplementedInitializer"
import SpriteKit
class GameScene: SKScene {
var paddlePositionUpdate:CGPoint
init(paddlePositionUpdate:CGPoint){
self.paddlePositionUpdate = CGPoint.zeroPoint
super.init()
}
}
The designated inititaliser for SKScene is init(size) where size is the size of your scene. You have attempted to use init() which does not exist.
You can see the exact method signature if you Cmd+click on SKScene...
EDIT: Try something like
class GameScene: SKScene {
var paddlePositionUpdate: CGPoint
init(size: CGSize, paddlePosition: CGPoint = CGPoint.zeroPoint){
paddlePositionUpdate = paddlePosition
super.init(size)
}
}
A scene should be initialised with a size...
I had a similar problem when replacing a scene with another one using
let gameScene = GameScene(size: self.size)
self.view.presentScene(gameScene)
And I kept getting 2 errors about initialization:
// Error #1
'GameScene' is not constructible with '(size: #lvalue CGSize)'
// Error #2
Class 'GameScene' has no initializers
To fix error #1, just replace GameScene(size: self.size) with GameScene.sceneWithSize(self.size)
Error #2 seems to be affected by how the property is initialized. For example, if I wrote:
class GameScene: SKScene {
var test: CGPoint
// ...
}
Error #2 was still there. But if I replaced var test: CGPoint with var test: CGPoint! , var test = CGPoint(0,0) , or any other lines may give test a certain value, Error #2 was gone. I searched the guide book but found lots of classes using lines like var test: CGPoint. And here I got confused.
It maybe a common problem for Swift beginners and I hope my temporary solution helps.