2015-09-12 99 views
1

我最近更新到Xcode 7 Beta,現在我收到一條錯誤消息「實例成員'視圖'不能在類型'GameScene'中用於第5行。有任何想法如何解決這個此外,如果你想成爲額外的幫助,請參閱我的其他問題:ConvertPointToView Function not working in Swift Xcode 7 Beta實例成員'視圖'不能在類型'GameScene'上使用

import SpriteKit 

class GameScene: SKScene { 

var titleLabel: StandardLabel = StandardLabel(x: 0, y: 0, width: 250, height: 80, doCenter: true, text: "Baore", textColor: UIColor.redColor(), backgroundColor: UIColor(white: 0, alpha: 0), font: "Futura-CondensedExtraBold", fontSize: 80, border: false, sceneWidth: view.scene.frame.maxX) 

override func didMoveToView(view: SKView) { 
    self.scene?.size = StandardScene.size 
    self.view?.addSubview(titleLabel) 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch in (touches) { 
     let location = touch.locationInNode(self) 
    } 
} 

override func update(currentTime: CFTimeInterval) { 
} 
} 
+0

什麼是StandardLabel? – ABakerSmith

+0

@ABakerSmith Line 5.不要擔心標準標籤,我會傳遞它的有效參數。我可以告訴你,如果你想,但它只是一堆初始化。問題是'view',我得到錯誤信息''view'不能用於類型'GameScene'「 –

+0

@ABakerSmith好吧,你使用的是Xcode beta 7嗎?不,StandardLabel是UILabel的一個子類。它與self.view?.addSubview(titleLabel)沒有任何關係。它僅適用於提到'視圖'時的第5行。 –

回答

9

您的問題是你是你的GameScene實例之前使用self已經完全初始化如果你拿。看看第5行的結尾:

var titleLabel = StandardLabel(..., sceneWidth: view.scene.frame.maxX) 
// Would be a good idea to use `let` here if you're not changing `titleLabel`. 

在這裏你參考self.view

爲了解決這個我會懶洋洋地初始化titleLabel

lazy var titleLabel: StandardLabel = StandardLabel(..., sceneWidth: self.view!.scene.frame.maxX) 
// You need to explicitly reference `self` when creating lazy properties. 
// You also need to explicitly state the type of your property. 

The Swift Programming Language: Properties,在慵懶的存儲性能:

A lazy stored property is a property whose initial value is not calculated until the first time it is used.

因此,您在didMoveToView使用titleLabel的時候,self已經完全初始化並且使用self.view!.frame.maxX是安全的(見下文如何達到相同的結果,而不需要強制解包view)。


編輯

考慮看看你的錯誤的圖片:

enter image description here

你的第一個問題是你需要使用懶惰時,明確規定物業類型變量。其次,你需要明確地引用自使用延遲屬性時:

lazy var label: UILabel = 
    UILabel(frame: CGRect(x: self.view!.scene!.frame.maxX, y: 5, width: 5, height: 5)) 

你可能有點雖然不使用viewscene打掃一下 - 你已經有了一個參考scene - 這是self

lazy var label: UILabel = 
    UILabel(frame: CGRect(x: self.frame.maxX, y: 5, width: 5, height: 5)) 
+0

這似乎沒有解決它。我通過不使用標準標籤簡化了它:http://i1087.photobucket.com/albums/j461/niiooo/Screen%20Shot%202015-09-12%20at%208.31.35%20PM.png –

+0

我的不好,我忘了'view'是一個可選項。我會更新我的答案。 – ABakerSmith

+0

非常感謝您的努力 –

相關問題