Inappropriate Expected declaration error

前端 未结 1 410
耶瑟儿~
耶瑟儿~ 2020-12-04 03:58

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

相关标签:
1条回答
  • 2020-12-04 05:01

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题