swift2 SKScene 类的对象如何将大小作为参数?
Posted
技术标签:
【中文标题】swift2 SKScene 类的对象如何将大小作为参数?【英文标题】:swift2 how can an object of SKScene class takes a size as a parameter? 【发布时间】:2015-10-31 01:48:28 【问题描述】:我正在学习教程
这是我的代码
class GameScene: SKScene
override func didMoveToView(view: SKView)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
override func update(currentTime: NSTimeInterval)
如您所见,有 no init 方法。虽然当我在我的视图控制器中这样做时:
//Create and configure the scene
scene = GameScene(size : skView.bounds.size)
场景在哪里:
var scene: GameScene!
它有效。尽管我没有编写代码,但 GameScene 如何批准具有大小的 init?
感谢您提供有关 SKSprite 的任何其他信息。我是这个框架的新手
【问题讨论】:
【参考方案1】:您的GameScene
是SKScene
的子类,因此您从SKScene
继承带有大小初始化程序的init。
如果你command-点击代码中的SKScene
,它会弹出类的头信息:
public class SKScene : SKEffectNode
/**
Called once when the scene is created, do your one-time setup here.
A scene is infinitely large, but it has a viewport that is the frame through which you present the content of the scene.
The passed in size defines the size of this viewport that you use to present the scene.
To display different portions of your scene, move the contents relative to the viewport. One way to do that is to create a SKNode to function as a viewport transformation. That node should have all visible conents parented under it.
@param size a size in points that signifies the viewport into the scene that defines your framing of the scene.
*/
public init(size: CGSize)
Xcode 中另一个有用的技术是 Option——单击函数调用以找出它们的定义位置。不幸的是,这对初始化程序不起作用,除非您在代码中明确调用它们。所以一个解决方法是改变:
var scene = GameScene(size : skView.bounds.size)
到
var scene = GameScene.init(size : skView.bounds.size)
然后选项-点击init
,会出现以下内容:
然后您可以点击SKScene Class Reference了解更多信息。
【讨论】:
以上是关于swift2 SKScene 类的对象如何将大小作为参数?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 SKScene 呈现 UIAlertController
将变量从 UIViewController 传递到 SK 场景