所以我用 init() 更新了代码,并使用了可选绑定(bind),正如您所说,它已经停止给我错误,但现在当我运行模拟时它甚至不会显示,这是什么原因?
此外,我需要它显示在名为“菜单”的不同场景中,并且想知道如何做到这一点?
import UIKit
import SpriteKit
class GameViewController: UIViewController {
var scrollView: UIScrollView!
convenience init() {
self.init()
if let tempVar = (scrollView) {
print("hello world")
}
else if scrollView != nil {
print("hello")
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
self.view.multipleTouchEnabled = true
// skView.showsPhysics = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
skView.presentScene(scene)
scrollView = UIScrollView(frame: view.bounds)
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 400)
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.scrollView!.frame = self.view.bounds
self.scrollView!.contentSize.height = 0
self.scrollView!.contentSize.width = 400
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
我做错了什么?
最佳答案
您已将 ScrollView (Scroller
) 定义为(隐式展开的)可选项。可选表示有一个值并且它等于 x,或者根本没有值。您必须使用可选绑定(bind)来安全地访问可选值。在隐式解包可选变量(scrollView!与scrollView?)的情况下,就像您的情况一样,不需要使用可选绑定(bind)来有条件地解包可选(因为它会在访问时隐式解包),但您必须采取当你尝试使用它时,请注意该变量不为零。
这里, Scroller
可能为零,因为您似乎还没有实际创建 UIScrollView
的实例(或者至少在您的代码)。
在访问其属性之前,您需要创建 UIScrollView
的实例:
Scroller = UIScrollView(frame: view.bounds)
另一件值得注意的事情是您正在使用 unarchiveFromFile
函数来创建场景。这是一种“旧”方式,您必须确保已在 SKNode 上的自定义扩展中定义此函数(因为在 Xcode 7 中,在默认 SpriteKit 游戏模板项目中无法再找到此扩展)。
此外,在使用 viewWillLayoutSubviews
时请注意,它可以被多次调用。
提示:类名应大写,而变量应以小写字母开头。
关于ios - SpriteKit,Swift 2.0 - Scrollview 给我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34494201/