简体   繁体   English

SpriteKit GameScene旋转错误

[英]SpriteKit GameScene Rotation Bug

I have been having a problem recently with this app which I encountered whilst trying to make the app universal and portrait orientation as well (the previous version was landscape on iPhone only). 我最近在遇到这个应用程序时遇到问题,同时尝试使该应用程序具有通用性和纵向性(以前的版本仅在iPhone上横向使用)。 Now the app will open in portrait and look correct, but it is still in portrait when the device is rotated. 现在,该应用将以纵向打开,并且看起来正确,但是在旋转设备时仍处于纵向。

应用程式错误

This is currently all the code I have for rotating the device (which works): 目前,这是我旋转设备的所有代码(有效):

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
    } else {
        return .All
    }
}

However, this only rotates the device and not the actual GameScene within which my game is contained. 但是,这只会旋转设备,而不会旋转包含我的游戏的实际GameScene。 If the app is launched from a landscape homescreen, the game will stay in landscape. 如果应用是从横向主屏幕启动的,则游戏将保持横向。

For testing purposes, I can quit and relaunch the app from the simulator homescreen and the game will work perfectly again until the device is rotated . 为了进行测试,我可以从模拟器主屏幕退出并重新启动该应用,然后游戏将再次正常运行, 直到设备旋转为止

How do I consistently present the GameScene in the correct orientation? 如何始终以正确的方向呈现GameScene?

You'll need to make sure that your SKView that holds your SKScene is set to match the bounds of it's containing frame. 你需要确保你的SKView保存您的SKScene设置与之相匹配的是一个包含帧的边界。 You can do this with Autolayout either in InterfaceBuilder or through code. 您可以在InterfaceBuilder中或通过代码使用“自动布局”来执行此操作。

As Good Doug said try setting the skView size. 正如Good Doug所说,请尝试设置skView大小。

In code you would load the first scene from the gameViewController like so. 在代码中,您将像这样从gameViewController加载第一个场景。 (This works if you dont use the scene sks files) (如果您不使用场景sks文件,则此方法有效)

    let skView = view as! SKView!
    let scene = StartScene(size: skView.bounds.size)

    skView.ignoresSiblingOrder = true
    scene.scaleMode = .AspectFill

    skView.presentScene(scene)

Also its seems like a very bad idea to make a game run in portrait and landscape, it will cause you so many problems I wouldn't even try. 同样,让游戏以纵向和横向运行似乎是一个非常糟糕的主意,它将给您带来很多我什至不会尝试的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM