-2

Yes, still very new to Swift, but in my current (first) app I started off positioning and sizing SKSpriteNodes in pixels. Now all of sudden I had to switch over to points, because everything was blowing up.

I have narrowed the cause to my applying the safeAreaLayount constraints, my GameViewControler code is at the bottom of this post.

My question is really two questions:

1 - Is there a way where I can properly tell Swift to use points instead of pixels (and vice versa)?

2 - Is there a reason to use one over another?

    import UIKit
    import SpriteKit
    import GameplayKit
    
    var originalS : UIView!
    
    var myGlobalVars = GlobalVars(backGround: SKSpriteNode(),
                                  pegHolder: SKSpriteNode(),
                                  widthPoints: 0.0, heightPoints: 0.0,
                                  widthPixels: 0.0, heightPixels: 0.0, passGo: false, sceneRect: .zero)
    
    class GameViewController: UIViewController {
    
        let logo = UIImage(named: "startup")
        override func viewDidLoad() {
            super.viewDidLoad()
            
            if let view = self.view as! SKView?
            {
    
    
                myGlobalVars.widthPixels = UIScreen.main.nativeBounds.width
                myGlobalVars.heightPixels = UIScreen.main.nativeBounds.height
                myGlobalVars.widthPoints = UIScreen.main.bounds.width
                myGlobalVars.heightPoints = UIScreen.main.bounds.height
    
                let imageView = UIView()
                originalS = imageView
                originalS.backgroundColor = .clear
                view.addSubview(originalS)
                
///////comment out this section and Swift uses pixels//////
                originalS.translatesAutoresizingMaskIntoConstraints = false
                originalS.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
                originalS.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
                originalS.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
                originalS.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor , constant: -0).isActive = true
///////comment out the section above and Swift uses pixels//////
                
                var scene : GameScene!
                DispatchQueue.main.async {
                     scene = GameScene(size: CGSize(width: originalS.frame.width,
                                                       height: originalS.frame.height))
                    scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
                    scene.backgroundColor = .black
                    scene.scaleMode = .aspectFit
                    myGlobalVars.sceneRect = scene.frame
                    view.presentScene(scene)
                }
                myGlobalVars.passGo = true
                
    
                //change these at end of development
                view.ignoresSiblingOrder = true
                view.showsFPS = true
                view.showsNodeCount = true
            }
        }
        
        override var shouldAutorotate: Bool {
            return false
        }
    
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            if UIDevice.current.userInterfaceIdiom == .phone {
                return .allButUpsideDown
            } else {
                return .all
            }
        }
    
        override var prefersStatusBarHidden: Bool {
            return false
        }
    }
Caractacus
  • 83
  • 7

1 Answers1

0

this is a similar question I think

so:

  1. Is there a way where I can properly tell Swift to use points instead of pixels (and vice versa)? Answer: all of these transformations are going automatically because we have 1x, 2x, 3x pixels screen size and the system knows which image it should show. So the system manages the different images and you are always working with points. (system know when she is on 2x screen device or 3x screen devices and which image it should use.

  2. Is there a reason to use one over another? Answer: You just need to use points all the time. (and provider image for all screen types 1x, 2x, 3x image. For example, you have a block with image 100X100. You need to add 3 images to your project - 100X100 200X200 300X300, and system will put needed image depend on screen type

  • OK, I understand. But I guess I am still confused about **setting** pixels vs. points. Had I not went back to look at safeArea constraints the code was all written in pixels and that was fine. – Caractacus Aug 18 '20 at 03:51
  • All code is always written in point. I think it is just you decide that it is in pixels. Can you maybe provider code which "written in pixels", because you will never set "pixel unit" in the code it is always a "points". Because even now we have e resolutions. Tomorrow Apple will create 4x and 5x screens and all app should work as earlier, and you will just add new 4x 5x images to your old projects. – Alexander Nikolaychuk Aug 18 '20 at 03:57
  • I put my entire GameViewControler file in the OP. If you comment out the 5 lines that I highlighted and deal with safeAreaLayout, then the call where I create GameScene will read the W&H as pixels, not points. – Caractacus Aug 18 '20 at 04:12