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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mbarab2
  • 1
  • 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 – mbarab2 Apr 27 '19 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. – Paulw11 Apr 27 '19 at 23:46
  • 1
    Possible duplicate of [fatal error: init(coder:) has not been implemented error despite being implemented](https://stackoverflow.com/questions/38966565/fatal-error-initcoder-has-not-been-implemented-error-despite-being-implement) – elarcoiris Apr 28 '19 at 01:28
  • See the answer in this question: https://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") – elarcoiris Apr 28 '19 at 01:29

2 Answers2

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)

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.

kid_x
  • 1,415
  • 1
  • 11
  • 31