0

Unfortunately I get the error message 'self' used in property access 'healthPoints' before 'super.init' call again and again with my code:

First Class Unit

import UIKit
import SpriteKit

class Unit {

//    var gameScene : GameScene!

    var healthPoints = 10
    var damage = 5
    var movement = 1

    init(pHealthPoints: Int, pDamage: Int,pMovement: Int) {

        self.healthPoints = pHealthPoints
        self.damage = pDamage
         self.movement = pMovement
    }


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

}

Subclass Knight

import UIKit
import SpriteKit

class Knight: Unit {

    override init(pHealthPoints: Int, pDamage: Int, pMovement: Int) {

        self.healthPoints = pHealthPoints
        self.damage = pDamage
        self.movement = pMovement

    }

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

}

Where exactly do I have to write super in the subclass?

How can I access the class Unit or the class Knight via the GameScene.swift or create an object of the class Knight?

For each answer I am very grateful

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
TimKrs
  • 67
  • 1
  • 9
  • Possible duplicate of https://stackoverflow.com/questions/33765485/use-of-self-in-method-call-before-super-init-initializes-self-cant-init-prop/33765813 or https://stackoverflow.com/questions/41779219/use-of-self-in-property-access-frame-before-super-init-initializes-self or https://stackoverflow.com/questions/29890510/property-self-not-initialized-at-super-init-call – Martin R Feb 13 '19 at 20:34
  • Re your error, just make sure to call `super.init` before you try updating any of these properties. Or better, since `Knight` `init` is just updating the exact same properties, just call `super.init` with those parameters and you’re done: https://gist.github.com/robertmryan/dde5d1b2addf210b87132c9b9534d54e. Or, because you’re not doing anything new in either of these `init` methods, you can remove them entirely from `Knight` and you’ll inherit whatever `Unit` does. – Rob Feb 14 '19 at 00:14
  • Possible duplicate of ['Use of self in method call before super.init initializes self', can't init properties through a method call](https://stackoverflow.com/questions/33765485/use-of-self-in-method-call-before-super-init-initializes-self-cant-init-prop) – Cristik Feb 14 '19 at 00:38

1 Answers1

2

You simply need to call super.init before you would assign any values to the properties of your subclass. There's also no need to implement init?(coder aDecoder: NSCoder). Also keep in mind that your overriden init method doesn't actually do anything different than the super implementation, so there's no point in overriding it.

class Knight: Unit {

    override init(pHealthPoints: Int, pDamage: Int, pMovement: Int) {
        super.init(pHealthPoints: pHealthPoints, pDamage: pDamage, pMovement: pMovement)
        self.healthPoints = pHealthPoints
        self.damage = pDamage
        self.movement = pMovement
    }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • Now a mistake occurs that I've never had before. I'm being suggested that I should change `let scene = GameScene(size: self.view.bounds.size)` with`let scene = GameScene(coder: self.view.bounds.size)` in `GameViewController.swit`. But there follows another error message `Cannot convert value of type 'CGSize' to expected argument type 'NSCoder'` – TimKrs Feb 13 '19 at 20:46
  • @TimKrs that code has nothing to do with the code in your question. Update the question with the actual code producing that error in the form of a [mcve]. – Dávid Pásztor Feb 13 '19 at 20:48