我想创建一个位于每个设备左上角的SKLabelNode。此外,在左上角标签的左侧,我想要一个硬币图像,以显示这是当前的硬币计数标签。每次我将标签放在iPhone 6s +的角落里时,它都不在iPad的角落里。
到目前为止,这是我的代码:
cornerCoin.position = CGPoint(x: screenWidth / -3.1, y: screenHeight / 3.175)
cornerCoin.zPosition = 10
cameraNode.addChild(cornerCoin)
coinLabel.position = CGPoint(x: screenWidth / -2.4, y: screenHeight / 8)
coinLabel.zPosition = 1
coinLabel.fontSize = 50
coinLabel.fontColor = UIColor.black
coinLabel.fontName = "04b19"
cameraNode.addChild(coinLabel)
答案 0 :(得分:1)
就我个人而言,我不会跟踪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)
}
我们的标签现在应位于相机的右上方位置,硬币应位于我们标签的左侧,与文本垂直居中。
如果您不喜欢顶部对齐的外观,您可能想要了解标签的位置。