Subclass of SpriteKit classes in Swift

I am very new to iOS development, but I played with the SpriteKit template app to find out how everything works, and try to boot into Swift while I am in it. The only thing I came across was how to work with subclasses of SpriteKit.

I am in the GameScene.swift file and I am trying to extract a class for the Hello World shortcut, so here is what this file looks like:

// GameScene.swift import SpriteKit class HelloLabel: SKLabelNode { init(fontNamed: String) { super.init(fontNamed: fontNamed) self.text = "Hello, World!" self.fontSize = 65; self.position = CGPoint(x: 400, y: 500); } } class GameScene: SKScene { override func didMoveToView(view: SKView) { /* Setup your scene here */ // let myLabel = SKLabelNode(fontNamed:"Chalkduster") // myLabel.text = "Hello, World!"; // myLabel.fontSize = 65; // myLabel.position = CGPoint(x: 400, y: 500); let myLabel = HelloLabel(fontNamed: "Chalkduster") self.addChild(myLabel) } override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { /* snip, no changes made here */ } override func update(currentTime: CFTimeInterval) { /* snip, no changes made here */ } } 

So, HelloLabel is just for going through, to understand how everything gets together, but when I run the application, I get the following error:

 /Users/jon/Projects/ErrorExample/ErrorExample/GameScene.swift: 11: 11: fatal error: use of unimplemented initializer 'init()' for class 'ErrorExample.HelloLabel' 

I do not understand what this message is trying to tell me. The way I read this error is that it complains that I did not implement an initializer called init in the ErrorExample.HelloLabel class, but it seems to look like I have it!

So what am I doing wrong here - how to extract a class to hide all this setting?

+6
source share
4 answers

I donโ€™t know exactly why, but the hidden functionality inside SKLabelNode tried to call the init function without parameters. This seems to work:

 class HelloLabel: SKLabelNode { init() { super.init() } init(fontNamed fontName: String!) { super.init(fontNamed: fontName) self.text = "Hello, World!" self.fontSize = 65; self.position = CGPoint(x: 400, y: 500); } } 
+11
source

This works better:

 class LinkLabel: SKLabelNode { override init() { super.init() } override init(fontNamed fontName: String!) { super.init(fontNamed: fontName) self.text = "Hello, World!" self.fontSize = 65; self.position = CGPoint(x: 400, y: 500); } required init(coder aDecoder: NSCoder!) { super.init() } } 
+2
source

Perhaps because the superclass expects changing NSString* from init(fontNamed: String) { to init(fontNamed: NSString) { will solve the problem?

Or another feature can be used with the obj-c bridge:

 init(fontNamed: String) { super.init(fontNamed: fontNamed.bridgeToObjectiveC()) ... } 
0
source

You must first initialize your class and then initialize the superclass. So your init() should look like this:

 init(fontNamed: String) { self.text = "Hello, World!" self.fontSize = 65; self.position = CGPoint(x: 400, y: 500); super.init(fontNamed: fontNamed) } 

This is due to security and is explained in session 403 of the WWDC, Intermediate Speed .

-1
source

Source: https://habr.com/ru/post/970459/


All Articles