问题
I am currently experimenting in Swift Playgrounds, and am trying out SpriteKit. My playground is working as expected, and runs, but Xcode has an error saying "Use of unresolved identifier 'myClass'". The playground still runs perfectly still though.
Here is my code:
import UIKit
import PlaygroundSupport
import SpriteKit
let frame = CGRect(x: 0, y: 0, width: 800, height: 600)
let mySKScene = myClass()
let view = SKView(frame: frame)
view.presentScene(mySKScene)
PlaygroundPage.current.liveView = view
class myClass: SKScene {
override func didMove(to view: SKView) {
}
}
Thanks in advance for any help.
P.S. I am using the iOS SKD
回答1:
myClass has not been defined at the time you are trying to user it. Change your code to look like this:
import UIKit
import PlaygroundSupport
import SpriteKit
class myClass: SKScene {
override func didMove(to view: SKView) {
}
}
let frame = CGRect(x: 0, y: 0, width: 800, height: 600)
let mySKScene = myClass()
let view = SKView(frame: frame)
view.presentScene(mySKScene)
PlaygroundPage.current.liveView = view
Just a note about styling. Classes are usually declared with the first letter being capitalized.(ie. class MyClass: SKScene {}
)
来源:https://stackoverflow.com/questions/42728404/swift-playground-use-of-unresolved-identifier-myclass-but-still-compiles