0

Picture of code

I don't understand how to fix the error. If someone understands please help!

class GameScene: SKScene {
    let player = SKSpriteNode(imageNamed: "playerShip")
    let bulletSound = SKAction.playSoundFileNamed("bulletSound.wav", waitForCompletion: false)

    var gameArea: CGRect

    override init(size: CGSize)
    {
        let maxAspectRatio: CGFloat = 16.0/9.0
        let playableWidth = size.height/maxAspectRatio
        let margin = (size.width - playableWidth)/2
        gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height)
        super.init(size: size)
    }

    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }

I'm trying to set boundaries for the objects on the screen. When I wrote super.init(size: size), Xcode automatically created required init?(coder aDecoder: NSCoder), but when it runs I get the error of:

Thread 1: Fatal error: init(coder:) has not been implemented

4
  • I just added some more information. I implemented the code Xcode told me to, but when I run it I keep getting the Fatal error Commented Apr 27, 2019 at 23:03
  • Presumably, your scene is contained in a storyboard. The init(coder:) initialiser is used in this case, not init(size:). You could add the required code to the init(coder) initialiser, but it is probably better to add appropriate constraints to your storyboard - An aspect ratio constraint of your scene's width to its height, along with top and bottom constraints would probably work. Commented Apr 27, 2019 at 23:46
  • 1
    Possible duplicate of fatal error: init(coder:) has not been implemented error despite being implemented Commented Apr 28, 2019 at 1:28
  • See the answer in this question: stackoverflow.com/a/38966583/4844273 It may be a case of just adding the line super.init(coder: aDecoder) above the line: fatalError("init(coder:) has not been implemented") Commented Apr 28, 2019 at 1:29

2 Answers 2

0

Because you have overridden the designated initializer for SKScene with your override init(size: CGSize) method and therefore have to implement this required init?(coder aDecoder: NSCoder) method to allow the custom SKScene class (your GameScene class), object serialization.

This older article might help explain this for you:

What exactly is init coder aDecoder?

If you are trying to initialize the gameArea variable, why not just try something like setting gameArea in an override of sceneDidLoad()? (really awful code below):

class GameScene: SKScene {
    let player = SKSpriteNode(imageNamed: "playerShip")
    let bulletSound = SKAction.playSoundFileNamed("bulletSound.wav", waitForCompletion: false)

    var gameArea: CGRect?

    override func sceneDidLoad() {
        let maxAspectRatio: CGFloat = 16.0/9.0
        let playableWidth = size.height/maxAspectRatio
        let margin = (size.width - playableWidth)/2

        gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height)
    }

    <<more of your code>>
}

and eliminate the override init(size: CGSize)

Sign up to request clarification or add additional context in comments.

Comments

0

Like oaccamsrazor states, if you add an initializer to your class, you also have to implement any required initializers in the superclass. The fact that you're hitting init?(coder aDecoder: NSCoder) indicates init(size: ...) isn't getting called, likely because you're creating your scene from an .sks file.

init?(coder aDecoder: NSCoder) will get called when creating your scene from an .sks file, so you can't have fatalError() there. Basically, just actually implement your initializer if you have anything to initialize. Otherwise, delete all your initializers and the requirement to have init?(coder aDecoder: NSCoder) will disappear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.