swift - 它一直告诉我 "Class ' GameScene' 没有初始化程序”

标签 swift class

import SpriteKit

class GameScene: SKScene {
    var movingGround: JTMovingGround!
    var hero: JTHero

    override func didMoveToView(view: SKView) {
       backgroundColor = UIColor(red: 159.0/255.0, green: 281.0/255.0, blue: 244.0/255.0, alpha: 1.0)
       movingGround = JTMovingGround(size: CGSizeMake(view.frame.width, 20))
        movingGround.position = CGPointMake(0, view.frame.size.height/2)
        addChild(movingGround)

        hero = JTHero()
        hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.height/2)
        addChild(hero)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        movingGround.start()
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

谁能告诉我哪里出了问题??谢谢

最佳答案

这是因为您有一个没有默认值的非可选属性。

var hero: JTHero

是非可选的,但也没有值。

因此,您可以通过以下方式将其设为可选

var hero: JTHero?

或者您可以创建一个 init 方法并在其中设置值。

或者创建一个默认值...

var hero = JTHero()

有很多方法可以做到后者。

关于swift - 它一直告诉我 "Class ' GameScene' 没有初始化程序”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30771918/

相关文章:

swift - 水平和垂直滚动 UITableView

java - 在Java中创建具有多种数据形式的多维数组

c# - 结构、枚举、类、接口(interface),还有什么?

javascript - 将对象作为函数调用 JavaScript

swift - 如何在 Swift 中为结构体的项目定义标签?

swift - 无法调用非函数类型的值 'DrawCircleSegment'

random - 将 arc4random() 的结果转换为 Int 时崩溃

C# 奇怪的对象行为

python - 尝试将类(在列表中)保存到文件中

ios - 如何使用打开的 ViewController 启动计时器?