Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

sprite kit - Background is not filling the whole view SpriteKit

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)

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

Why is my code cutting off the tops and the bottoms of the backsimulator screen shotground?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Edit: 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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...