是的,对Swift来说还是很新的,但在我目前的(第一个(应用程序中,我开始以像素为单位定位和调整SKSpriteNodes。现在突然间,我不得不换到积分上,因为一切都很糟糕。
我已经将原因缩小到我应用了safeAreaLayount约束,我的GameViewControler代码在这篇文章的底部。
我的问题实际上是两个问题:
1-有没有一种方法可以让我正确地告诉Swift使用点而不是像素(反之亦然(?
2-有理由使用一个而不是另一个吗?
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
}
}
这是一个类似的问题,我认为
所以:
-
有没有一种方法可以让我正确地告诉Swift使用点而不是像素(反之亦然(?答:所有这些转换都是自动进行的,因为我们有1x、2x、3x像素的屏幕大小,系统知道它应该显示哪个图像。因此,系统管理不同的图像,并且您始终使用点。(系统知道她何时在2倍屏幕设备或3倍屏幕设备上,以及应该使用哪个图像。
-
有理由使用一个而不是另一个吗?答:你只需要一直使用积分。(以及适用于所有屏幕类型1x、2x、3x图像的提供商图像。例如,您有一个图像为100X100的块。您需要向您的项目添加3个图像-100X100 200X200 300X300,系统将根据屏幕类型放置所需的图像