2015-09-12 41 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

非常感谢您的努力 –

相关问题