Changing scene causes zoom

大兔子大兔子 提交于 2019-12-12 06:14:42

问题


When going from GameScene to PlayScene using this code below, all works fine:

for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if self.nodeAtPoint(location) == self.playButton1 {
                var scene = PlayScene(size: self.size)
                let skView = self.view as SKView!
                skView.ignoresSiblingOrder = true
                scene.scaleMode = .ResizeFill
                scene.size = skView.bounds.size
                skView.presentScene(scene)
            }
        }

But when i am going from PlayScene to GameScene using this code below, it gets zoomed in:

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            let skView = self.view as SKView!
            skView.ignoresSiblingOrder = true
            scene.scaleMode = .ResizeFill
            scene.size = skView.bounds.size
            skView.presentScene(scene)
        }

Tried to Updated code:

var scene = GameScene(size: self.size)
        let skView = self.view as SKView!
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .ResizeFill
        scene.size = skView.bounds.size
        skView.presentScene(scene)
}

How come? Any suggestions?


回答1:


In the first code snippet, you create the new scene of self.size which keeps the same size as the old one. But in the second one, you use another method to do that, and the size of the new scene is initialized from the file GameScene.sks whose size is 1024x768 by default.


EDIT:

Did you use

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {...}

to create your first scene in your GameViewController.swift? If so, try to replace this line with

let scene = GameScene(size: view.bounds.size)

I'm afraid the reason why your buttons get resized(zoomed) is that they are not in the right size at the first time you load GameScene, actually they are in a scene sized 1024x768. Your button is hard-coded as 400 width which is nearly the 1/3 of the scene width (1024pt). When you load GameScene again, its width now becomes the screen width, so you find buttons get resized. Print scene.size.width for more info.




回答2:


I had the same problem. For me the problem was that the scene size had changed to be much smaller when going back to the first scene.

Scene 1) GameMap Scene

  • Original Scene Size: (1024.0, 4096.0)
  • Original View Size : ( 320.0, 480.0)

Scene 2) GameLevel Scene

  • Original Scene Size: ( 320.0, 480.0)
  • Original View Size : ( 320.0, 480.0)

When Moving from Scene 1 to Scene 2 everything looked fine but when moving back from Scene 2 to Scene 1 The "Scene 1" size is changed to Scene 2's size

Scene 1) GameMap Scene -> After Going Back

  • Original Scene Size: ( 320.0, 480.0)
  • Original View Size : ( 320.0, 480.0)

My solution was to specifically change the size of the scene in "didMoveToView" and set the scaleMode of the "Scene 1" see below:

    self.view?.bounds = CGRectMake(0, 0, Constants.screenWidth, Constants.screenHeight)
    self.scene?.size = CGSizeMake(1024, 4096)

    self.scaleMode = SKSceneScaleMode.AspectFill

Hope it helps BR BuD



来源:https://stackoverflow.com/questions/31930786/changing-scene-causes-zoom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!