Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 无法使用类型为“(纹理:SKTexture,颜色:UIColor,大小:CGSize,()->())的参数列表调用“SKSpriteNode.init”_Swift_Sprite Kit_Skspritenode - Fatal编程技术网

Swift 无法使用类型为“(纹理:SKTexture,颜色:UIColor,大小:CGSize,()->())的参数列表调用“SKSpriteNode.init”

Swift 无法使用类型为“(纹理:SKTexture,颜色:UIColor,大小:CGSize,()->())的参数列表调用“SKSpriteNode.init”,swift,sprite-kit,skspritenode,Swift,Sprite Kit,Skspritenode,有人能帮我解决这个问题吗?即使我使用指定的初始值设定项,也会生成显示的错误 class OtherOrb: SKSpriteNode { override init() { let texture = SKTexture(imageNamed: "Orb") super.init(texture: texture, color: UIColor.clear, size: texture.size()){ self.position = CGPoint(x:

有人能帮我解决这个问题吗?即使我使用指定的初始值设定项,也会生成显示的错误

class OtherOrb: SKSpriteNode {

override init() {

    let texture = SKTexture(imageNamed: "Orb")

    super.init(texture: texture, color: UIColor.clear, size: texture.size()){
        self.position = CGPoint(x: 0.0, y: 500.0)
        self.physicsBody = SKPhysicsBody(circleOfRadius: 20) 
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

由于初始值设定项后面有闭包,因此出现此错误

在Swift中,编译器在方法调用之后直接将闭包解释为所述方法调用的最终参数,因此您得到的错误实际上表明不存在包含该闭包参数的初始化器

此外,你的代码看起来很不对劲,我不相信它是有效的。你想完成什么

基于我的怀疑,我认为您的代码应该类似于以下代码:

class OtherOrb: SKSpriteNode {

    init() {

        let texture = SKTexture(imageNamed: "Orb")
        super.init(texture: texture, color: UIColor.clear, size: texture.size())

        self.position = CGPoint(x: 0.0, y: 500.0)
        self.physicsBody = SKPhysicsBody(circleOfRadius: 20)
    }

    @available(*, unavailable)
    override init(texture: SKTexture?, color: UIColor, size: CGSize) {
        super.init(texture: texture, color: color, size: size)
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

也就是说,您想要一个没有参数的初始化器作为您指定的初始化器。

谢谢您,Jacob。你能解释一下可用的部件吗?我从来没有在任何关于堆栈溢出的示例或问题中看到过类似的情况。当然,通过将该方法标记为不可用,如果您尝试使用它,编译器会抱怨。此外,它不会被Xcode auto complete索引,因此它只会让开发体验变得更好。这是完全可选的,但不是必需的,只是我个人的喜好让required init不可用不是一个好主意,required的存在是有原因的,在大多数情况下我都会同意,尽管考虑到这个特定场景的上下文。如果调用初始化器,应用程序将终止。编码器初始化通常由IB或NSKeyedArchiver调用。考虑到这两种方法都不在这里使用,因此使该方法不可用是相当安全的。