我在GameScene中有一个标签,这里没有自动布局,我如何使用CGpoint来确保这个标签在不同的屏幕上保持在相同的位置。

下面是我放置分数标签的代码
scoreLabel?.position = CGPoint(x: -size.width / 2 + 120, y: size.height / 2 - 150)发布于 2020-07-04 13:57:09
let scoreLabel = //insert functionality of scoreLabel here
scoreLabel?.position = CGPoint(x: size.width + //insert dimention,
y: size.height + //insert dimension)
self.addChild(scoreLabel)发布于 2020-07-04 20:29:18
看到你之前的评论,我建议你使用以下函数,当它们在不同的屏幕尺寸上运行时,它们会改变尺寸:
AspectFill
AspectFit
ResizeFill这里有一个可以帮助你如何使用这些函数的链接:Sprite Kit Scene Editor GameScene.sks scene width and height
希望这能有所帮助!
发布于 2020-07-08 05:04:34
下面是我放在我的View Controller中的内容:
var h, w = 1000
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
w = view.frame.width / (view.frame.height / 1000)
let scene = GameScene(size: CGSize(width: w, height: h))
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}
}知道我把任何设备的高度设为1000是很方便的。让它变得更通用要容易得多。
然后我把所有的东西都按百分比显示在屏幕上。
label.position.x = w/2
label.position.y = h/2
label2.position.x = w/2
label2.position.y = h/2 - 50这几行代码将在屏幕中央放置两个标签。即使你旋转你的设备,即使你使用不同大小的设备。我使用的- 50将始终保持不变。
我的主要结论是:我创建屏幕相对大小的全局状态。
有用的提示:我总是把场景的锚点设为.zero。这样,最小宽度和高度始终为0,最大高度始终为1000。
https://stackoverflow.com/questions/62652311
复制相似问题