不恰当的预期声明错误 [英] Inappropriate Expected declaration error

查看:117
本文介绍了不恰当的预期声明错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我在这段代码中出错,这对我来说似乎很正常。我将'hills'声明为变量但我仍然收到'hills'和'bg'的错误预期声明。

Why am I getting an error in this code, it seems perfectly normal to me. I declare 'hills' as a variable but I am still getting the error "Expected declaration" for both 'hills' and 'bg'.

var bg = SKSpriteNode(imageNamed: "sky")
bg.position = CGPointMake(bg.size.width / 2, bg.size.height / 2)


self.addChild(bg)

var hills = SKSpriteNode(imageNamed: "hills")
hills.position = CGPointMake(hills.size.width / 2, 300)

self.addChild(hills)


推荐答案

我怀疑预期声明消息的原因是您已将此代码放在类中而不将其放在方法中。下面的示例编译,但如果它不包含在 setup 方法中,它会给出相同的错误:

I suspect the reason for the Expected Declaration message is that you have placed this code inside of a class without putting it inside of a method. The example below compiles, but it gives the same error you are seeing if it is not included inside of the setup method:

class MyNode: SKSpriteNode {
    func setup() {
        var bg = SKSpriteNode(imageNamed: "sky")
        bg.position = CGPointMake(bg.size.width / 2, bg.size.height / 2)


        self.addChild(bg)

        var hills = SKSpriteNode(imageNamed: "hills")
        hills.position = CGPointMake(hills.size.width / 2, 300)

        self.addChild(hills)
    }
}

这篇关于不恰当的预期声明错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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