Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing SpriteKit classes in Swift

Tags:

ios

swift

I'm very new to iOS development, but I've been playing around with the SpriteKit template app to learn how things work and try to boot up on Swift while I'm at it. One thing I'm having trouble with is how to work with SpriteKit subclasses.

I'm in the GameScene.swift file and I'm trying to extract a class for the "Hello World" label, so here's what that 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 intended to just be a pass-through in an attempt to understand how everything wires together, but when I run the app, 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'm not understanding what this message is trying to tell me. The way I read this error is that its complaining that I haven't implemented an initializer called init in the class ErrorExample.HelloLabel, but it sure looks like I have to me!

So, what am I doing wrong here - how does one extract a class to hide all this setup?

like image 329
jonallured Avatar asked Jun 06 '14 14:06

jonallured


People also ask

Does SpriteKit work with SwiftUI?

Even though the default Game Xcode Template creates the project based on a UIKit application, you can create a SwiftUI app and put your SpriteKit game inside it without any hustle thanks to the SpriteView view!

What is a SpriteKit?

SpriteKit is a general-purpose framework for drawing shapes, particles, text, images, and video in two dimensions. It leverages Metal to achieve high-performance rendering, while offering a simple programming interface to make it easy to create games and other graphics-intensive apps.

What is Apple SpriteKit?

SpriteKit is a powerful 2D sprite-based framework for games development from Apple. SpriteKit uses SKView which is a scene, it is the visual that you see on your screen. For those who are familiar with making iOS App, it is similar to Storyboard.

What is a node in SpriteKit?

Nodes are the fundamental building blocks of SpriteKit, and SKNode is the base class of all nodes. All of your onscreen assets will be an SKNode or a subclass thereof. SKNode s by themselves do not provide any visual content, however.


2 Answers

I'm not certain exactly why, but the hidden functionality inside SKLabelNode was trying to call an init function with no 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);
    }
}
like image 89
Dash Avatar answered Sep 29 '22 09:09

Dash


This seems to work 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()
    }

}
like image 37
zeeple Avatar answered Sep 29 '22 09:09

zeeple