Reputation: 301
I am new to swift and I am currently learning about initializers. I am running into an issue with overriding the parent class initializer. I get a compiler error "Use of 'self' in property access 'ImageTexture' before super init initializes self"
I would like a new image to get used when special badge class is called
here is the special badge class where I get the error
class SpecialBadge: Badge {
enum BadgeAnimation: Int {
case GrowAndShrink = 0, Rotate, Shake
}
override init(requestType: UDRequestType) {
let animation = BadgeAnimation(rawValue: Int(arc4random_uniform(3)))
self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
super.init(requestType: requestType)
switch animation! {
case .GrowAndShrink:
growShrink()
case .Rotate:
rotate()
case .Shake:
shake()
}
}
}
I get an error when I call on line:
self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
before calling super.init(requestType: requestType)
My parent class;
class Badge: SKSpriteNode {
var requestType: UDRequestType
var imageTexture: SKTexture?
init(requestType: UDRequestType) {
self.requestType = requestType
if let texture = imageTexture {
super.init(texture: texture, color: UIColor.clearColor(), size: CGSizeMake(48, 48))
} else {
let newTexture = SKTexture(imageNamed: "BadgeMagenta")
super.init(texture: newTexture, color: UIColor.clearColor(), size: CGSizeMake(48, 48))
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I can't figure out how to initialize imageTexture so it can use a different image before calling the initializer in SpecialBadge, any help would be greatly appreciated
Upvotes: 0
Views: 51
Reputation: 535231
It's just a matter of the order in which you do things. This is illegal:
self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
super.init(requestType: requestType)
This is legal:
super.init(requestType: requestType)
self.imageTexture = SKTexture(imageNamed: "BadgeTeal")
The rule is that you must complete initialization of the superclass before doing anything special to your own class.
Upvotes: 2