背景没有填满整个视图 SpriteKit [英] Background is not filling the whole view SpriteKit

查看:20
本文介绍了背景没有填满整个视图 SpriteKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我的代码不会填满整个 SKScene.这是我在 Xcode 12 Beta 5 上使用的代码.

For some reason my code will not fill the whole SKScene. Here is the code that I am using on Xcode 12 Beta 5.

GameScene.swift

class GameScene: SKScene {
 
    override func didMove(to view: SKView) {
        let background = SKSpriteNode(imageNamed: "space")
        background.zPosition = 0
        background.anchorPoint = CGPoint(x: 0.5, y: 0.5) // default
        background.position = CGPoint(x: frame.midX, y: frame.midY)
        print("frame.size  (frame.size)")
        print("self.size  (self.size)")
        print("view  (view.frame.size)")
        background.size = CGSize(width: self.size.width, height: self.size.height)
                self.addChild(background)
    }

}

GameViewController.swift

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        

        if let scene = GKScene(fileNamed: "GameScene") {
            
            if let sceneNode = scene.rootNode as! GameScene? {


                // Present the scene
                if let view = self.view as! SKView? {
                    sceneNode.size = view.bounds.size
                    sceneNode.anchorPoint = CGPoint.zero
                    sceneNode.scaleMode = .aspectFit
                    
                    print("view.bounds.size  (view.bounds.size)")
                    view.presentScene(sceneNode)
                    
                    view.ignoresSiblingOrder = true
                    
                    view.showsFPS = true
                    view.showsNodeCount = true
                }
            }
        }
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

另外由于某种原因,我的视图大小正在报告 ->

Also for some reason, my view size is reporting ->

frame.size (320.0, 480.0) self.size (320.0, 480.0) view (320.0,480.0)

frame.size (320.0, 480.0) self.size (320.0, 480.0) view (320.0, 480.0)

但我的 GameScene.sks 设置为 ->w750、h1336

But my GameScene.sks is set to -> w750, h1336

为什么我的代码会切断背面的顶部和底部ground?

Why is my code cutting off the tops and the bottoms of the background?

推荐答案

这听起来很愚蠢,但你有启动屏幕吗?我遇到了同样的问题,它只会在 Xcode 12 而不是 11 中发生,但我发现的主要区别是 Xcode 11 有一个启动屏幕.一旦我添加了启动屏幕故事板并将其添加到我的 plist 中,我的 SCNView 就会填满屏幕.我不确定为什么这会导致视图不遵循约束,但在将其添加到我的其他项目后,它解决了这个问题.

This is going to sound dumb, but do you have a launch Screen? I was having the same problem which would only happen in Xcode 12 and not 11, but the main difference I found was Xcode 11 has a launch screen. Once I added the launch screen storyboard and added it to my plist my SCNView would fill the screen. I am not sure why this would cause the view not to follow the constraints, but after adding it to my other projects it fixes the issue.

您不需要启动屏幕故事板,您只需在启动屏幕界面文件基本名称"下的 plist 中添加显示场景的主故事板.

You do not need to have a launch screen storyboard, you can just add your main storyboard that displays you scene in the plist under "Launch screen interface file base name".

这篇关于背景没有填满整个视图 SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆