简体   繁体   English

无法构建游戏场景,因为它没有可访问的初始化程序

[英]Game Scene cannot be constructed because it has not accessible initializers

Xcode is dropping an error and I dont know how to solve it. Xcode删除错误,我不知道如何解决。 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! 一旦我想用let passenger: PassengerNode!替换let passenger = SKSpriteNode(imageNamed: "passenger") 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. 我要更改它的原因是,我想将其替换为一个类,以便乘客a。 can have a different type with a different texture (this can may be solved differently) b. 可以具有不同的类型和不同的纹理(可以用不同的方法解决)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. 如果只声明其类型而没有初始值的乘客,则需要在类中添加init(),因为编译器不知道在创建对象时要赋予其初始值。

You can either create an init() and set a value to passenger or delare it as 您可以创建一个init()并将值设置为passenger或将其删除为

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). (这假设您的代码在确保将其放入变量之前未引用该变量,或者在使用它之前检查nil / optional)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法构造&#39;UITabBarDelegate&#39;,因为它没有可访问的初始化程序 - 'UITabBarDelegate' cannot be constructed because it has no accessible initializers 无法构造委托,因为它没有可访问的初始化程序? - Delegate cannot be constructed because it has no accessible initializers? 无法构造“ CLLocationManagerDelegate”,因为它没有可访问的初始化程序 - 'CLLocationManagerDelegate' cannot be constructed because it has no accessible initializers 无法构建&#39;DepartmentDataDelegate&#39;,因为它没有可访问的初始化程序 - 'DepartmentDataDelegate' cannot be constructed because it has no accessible initializers 枚举:“无法构造,因为它没有可访问的初始值设定项” - Enumeration: “cannot be constructed because it has no accessible initializers” 无法构造“任务”,因为它没有可访问的初始值设定项 - 'Task' cannot be constructed because it has no accessible initializers 初始化器类型无法构造,因为它没有可访问的初始化器 - Initializers Type Cannot be constructed because it has no accessible initializer Swift:Enum&#39;无法构造,因为它没有可访问的初始值设定项&#39; - Swift : Enum 'cannot be constructed because it has no accessible initializers' 错误:'结果<data, foursquareerror> ' 无法构造,因为它没有可访问的初始值设定项</data,> - Error: 'Result<Data, FourSquareError>' cannot be constructed because it has no accessible initializers Swift 错误:无法构造“Human”,因为它没有可访问的初始值设定项 - Swift Error: 'Human' cannot be constructed because it has no accessible initializers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM