就我个人而言,我不会跟踪 screenWidth/screenHeight,它与 SpriteKit 的基础相违背(你的场景就像你的虚拟屏幕,这就是你应该如何处理其中的所有内容)相反,你应该做什么从屏幕转换到场景,这样无论缩放模式或锚点如何,您的标签都在您想要的位置。
要将视图的右上角转换为场景,您可以这样做
if let view = scene.view
{
let topRightPos = view.convert(CGPoint(x:view.frame.maxX,y:view.frame.minY),to:scene)
let camTopRight = scene.convert(topRightPos,to:cameraNode)
}
现在我们知道我们的右上角位置在哪里:
我们可以的
if let view = scene.view
{
let topRightPos = view.convert(CGPoint(x:view.frame.maxX,y:view.frame.minY),to:scene)
let camTopRight = scene.convert(topRightPos,to:cameraNode)
coinLabel.position = camTopRight
coinLabel.zPosition = 1
coinLabel.fontSize = 50
coinLabel.fontColor = UIColor.black
coinLabel.fontName = "04b19"
coinLabel.horizontalAlighmentMode = .right
coinLabel.verticalAlighmentMode = .top
cameraNode.addChild(coinLabel)
}
右对齐我们的标签,使文本始终向左延伸,然后添加硬币,我们这样做:
if let view = scene.view
{
let topRightPos = view.convert(CGPoint(x:view.frame.maxX,y:view.frame.minY),to:scene)
let camTopRight = scene.convert(topRightPos,to:cameraNode)
coinLabel.position = camTopRight
coinLabel.zPosition = 1
coinLabel.fontSize = 50
coinLabel.fontColor = UIColor.black
coinLabel.fontName = "04b19"
coinLabel.horizontalAlighmentMode = .right
coinLabel.verticalAlighmentMode = .top
cameraNode.addChild(coinLabel)
cornerCoin.position = CGGPoint(x:0.0,y:coinLabel.frame.midY)
cornerCoin.anchorPoint = CGPoint(x:1.0,y:0.5)
cornerCoin.zPosition = 10
cameraNode.addChild(cornerCoin)
}
我们的标签现在应该在我们相机的右上角,硬币应该在我们标签的左边,与文本垂直居中。
如果您不喜欢顶部对齐的外观,您可能想尝试一下标签的定位方式。