Enjoy full screen resolution (SpriteKit x Swift4)

Asked 2 years ago, Updated 2 years ago, 90 views

According to the code I wrote, when the screen is full, only the lower left side is shown, and depending on the display I use, the display will be strange.
For example, is there a way to keep 800px*600px when you have a full screen with 800px*600px View and a window?There may be problems such as poor image quality, but I don't care about the current situation.

This is the property of the current View

swift4 spritekit

2022-09-30 16:48

2 Answers

Similar to UIImageView being able to display UIImage in different ways than the view size, SKView is able to display SKScene in different sizes.

The following code example is a modified Game/SpriteKit template project for iOS in Xcode 10.3.(I'm sorry, but I don't have the old Xcode, so it's Swift5, not Swift4)

It is easier to tell the difference by specifying an extreme value, so we set it to 80x60 instead of 800x600.

GameViewController.swift:

import UIKit
import SpriteKit

classGameViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()

        iflet view=self.view as!SKView? {
            // Ignore view size and create fixed size `GameScene`(`SKScene`)
            let screen = GameScene (size: CGSize (width: 80, height: 60))

            Scene.scaleMode=.fill//<-`scene` is ignored and expanded/reduced across views

            view.presentScene(scene)

            view.ignoresSiblingOrder=true

            view.showsFPS=true
            view.showsNodeCount=true
        }
    }

    override var shouldAutorotate:Bool {
        return true
    }

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

    override varprefersStatusBarHidden:Bool{
        return true
    }
}

GameScene.swift:

import SpriteKit

classGameScene:SKScene{

    private var label —SKLabelNode?

    override func DidMove (to view:SKView) {

        self.label=SKLabelNode(text: "Hello")
        iflet label =self.label {
            label.fontColor=.red
            label.verticalAlignmentMode=.center
            label.position = CGPoint (x:40, y:30)
            label.alpha = 0.0
            label.run (SKAction.fadeIn (withDuration: 2.0))
            addChild(label)
        }
    }

    override functouchesBegan(_touches:Set<UITouch>, with event:UIEvent?){
        iflet label =self.label {
            label.run(SKAction(named: "Pulse")!, withKey: "fadeInOut")
        }
    }
}

The Xcode template has SKView displayed in full size, so try running simulators with various resolutions and changing the position of SKLabelNode to see if it works.

However, your problem seems to be more of a layout issue for SKView."You said ""I wrote the code,"" but you didn't show me a single line of code, so I might have explained something wrong."

If this answer doesn't help you solve the problem, edit your question to learn more about "Current Code and Storyboard/xib Settings," "Expected View," "Current View," and more.


2022-09-30 16:48

There is no method that is so convenient, so how about this method?

  • View is constrained by AutoLayout to keep the View aspect ratio constant
  • WindowDelegate corresponds to the WindowController and retrieves the current window size in WindowDidResize,WindowDidEnterFullScreen
  • Create a Singleton-like ScalingManager and update the scaling factor (factor) based on the current View value when the window size changes
  • Program internal values are treated as theoretical values based on the base window size, and actual drawing returns real coordinates by passing coordinates and sizes to ScalingManager
  • Drawing to actual coordinates returned from ScalingManager

By having theoretical and real coordinates separately,

  • Logic is theoretical coordinates
  • Drawing is based on theoretical coordinates

If I do, I think I can handle changing the window size without changing the current logic significantly


2022-09-30 16:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.