当我进入场景时,如何设置场景的缩放模式?

时间:2015-09-30 15:25:47

标签: ios sprite-kit swift2 skscene

我的问题是当我试图切换到一个场景时,它的视图与我的开始场景不同。

问题是 - 我认为 - 比例模式,所以我尝试设置比例模式,但它不起作用。这就是我的尝试:

let skView = self.view! as SKView

let gameScene:GameScene = GameScene(size: self.frame.size)
gameScene.size = skView.bounds.size
gameScene.scaleMode = SKSceneScaleMode.ResizeFill

skView.presentScene(gameScene , transition: SKTransition.crossFadeWithDuration(1))

而且:

let gameScene = GameScene(size: self.size)

gameScene.scaleMode = SKSceneScaleMode.ResizeFill

self.view?.scene!.presentScene(gameScene , transition: SKTransition.crossFadeWithDuration(1))

我试图在didMoveToView()函数中设置场景的缩放模式。

这些方法都没有帮助,有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

这是一个从一个场景过渡到另一个场景的例子。假设GameMenu是您的第一个场景,并且您希望转换为GameScene

GameViewController 中初始化GameMenu尺寸:

override func viewDidLoad() {
    super.viewDidLoad()

    let scene = GameMenu(size: view.bounds.size)
    // Configure the view.
    let skView = self.view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = 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

    skView.presentScene(scene)
}

现在,你应该在第一个场景。何时转换为GameScene,请根据需要添加以下代码:

let nextScene = GameScene(size: self.size)
scene?.view?.presentScene(nextScene, transition: SKTransition.crossFadeWithDuration(1))

现在一切都已完成,GameScene出现了。如果您有任何问题,请检查此sample project

答案 1 :(得分:0)

override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = GameMenu(fileNamed:"GameMenu") {
            // Configure the view.
            let skView = self.view as! SKView
                        skView.showsFPS = true
                        skView.showsNodeCount = 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 = .ResizeFill

            skView.presentScene(scene)
        }
    }