Swift - 必须调用超类 SKSpriteNode 错误的指定初始化程序 [英] Swift - Must call a designated initializer of the superclass SKSpriteNode error

查看:21
本文介绍了Swift - 必须调用超类 SKSpriteNode 错误的指定初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在第一个 XCode 6 Beta 上工作,但在最新的 Beta 上它不工作并给出这样的错误必须调用超类 SKSpriteNode 的指定初始化程序:

This code worked on first XCode 6 Beta, but on latest Beta it's not working and gives such errors Must call a designated initializer of the superclass SKSpriteNode:

import SpriteKit

class Creature: SKSpriteNode {
  var isAlive:Bool = false {
    didSet {
        self.hidden = !isAlive
    }
  }
  var livingNeighbours:Int = 0

  init() {
    // throws: must call a designated initializer of the superclass SKSpriteNode
    super.init(imageNamed:"bubble") 
    self.hidden = true
  }

  init(texture: SKTexture!) {
    // throws: must call a designated initializer of the superclass SKSpriteNode
    super.init(texture: texture)
  }

  init(texture: SKTexture!, color: UIColor!, size: CGSize) {
    super.init(texture: texture, color: color, size: size)
  }
}

这就是这个类的初始化方式:

and that's how this class is initialiazed:

let creature = Creature()
creature.anchorPoint = CGPoint(x: 0, y: 0)
creature.position = CGPoint(x: Int(posX), y: Int(posY))
self.addChild(creature)

我被它困住了.. 最简单的解决方法是什么?

I'm stuck with it.. what will be the easiest fix?

推荐答案

init(texture: SKTexture!, color: UIColor!, size: CGSize)是SKSpriteNode类中唯一指定的初始化器,其余的都是便利初始化器,所以你不能对它们调用 super .将您的代码更改为:

init(texture: SKTexture!, color: UIColor!, size: CGSize) is the only designated initializer in the SKSpriteNode class, the rest are all convenience initializers, so you can't call super on them. Change your code to this:

class Creature: SKSpriteNode {
    var isAlive:Bool = false {
        didSet {
            self.hidden = !isAlive
        }
    }
    var livingNeighbours:Int = 0

    init() {
        // super.init(imageNamed:"bubble") You can't do this because you are not calling a designated initializer.
        let texture = SKTexture(imageNamed: "bubble")
        super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
        self.hidden = true
    }

    init(texture: SKTexture!) {
        //super.init(texture: texture) You can't do this because you are not calling a designated initializer.
        super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
    }

    init(texture: SKTexture!, color: UIColor!, size: CGSize) {
        super.init(texture: texture, color: color, size: size)
    }
}

此外,我会将所有这些合并到一个初始化程序中.

Furthermore I would consolidate all of these into a single initializer.

这篇关于Swift - 必须调用超类 SKSpriteNode 错误的指定初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆