简体   繁体   中英

Game Scene cannot be constructed because it has not accessible initializers

Xcode is dropping an error and I dont know how to solve it. Maybe someone can give me a hand or a good hint?

Here is my code that is working within my Game Scene:

var currentLevel: Int = 1

var platforms: PlatformsNode!
var platformNode1: PlatformNode!
var platformNode2: PlatformNode!
var platformNode3: PlatformNode!

var platformArray = [SKNode]()

let passenger = SKSpriteNode(imageNamed: "passenger")

var passengerNumber:Int = 1
var startPlatform =         [3, 3, 2, 1, 2]
var destinationPlatform =   [1, 1, 3, 2, 1]

// Level selection
class func level(levelNum: Int) -> GameScene? {
    let scene = GameScene(fileNamed: "Level\(levelNum)")! // <- compiler error
    scene.currentLevel = levelNum
    scene.scaleMode = .AspectFill
    return scene
}

As soon as I want to replace let passenger = SKSpriteNode(imageNamed: "passenger") with let passenger: PassengerNode! the compiler drops an compiler error "Game Scene cannot be constructed because it has not accessible initializers". And this error just shows up, when I change the way I want to declare let passenger. The reason I want to change it, is that I want to replace it with a class, so that the passenger a. can have a different type with a different texture (this can may be solved differently) b. can be removed from the parent and later be added again - with hopefully no error.

You got any ideas? I am really stuck somehow :-?

declaring passenger with only its type and no initial value requires that you add an init() to your class because the compiler does not know what initial value to give it when the object is created.

You can either create an init() and set a value to passenger or delare it as

let passenger: PassengerNode! = nil

(this assumes that your code does not reference the variable before making sure to put something in it or checks for nil/optional before using it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM