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
}
}