Ios 如何在我的通用应用程序中的每个设备的角落放置硬币标签?
我想在每个设备的左上角创建一个SKLabelNode。另外,在左上角标签的左侧,我想要一个硬币图像,显示这是当前的硬币计数标签。每次我把标签放在iphone6s+的角落里,它就不在iPad的角落里了 以下是我目前的代码:Ios 如何在我的通用应用程序中的每个设备的角落放置硬币标签?,ios,swift,sprite-kit,sklabelnode,Ios,Swift,Sprite Kit,Sklabelnode,我想在每个设备的左上角创建一个SKLabelNode。另外,在左上角标签的左侧,我想要一个硬币图像,显示这是当前的硬币计数标签。每次我把标签放在iphone6s+的角落里,它就不在iPad的角落里了 以下是我目前的代码: cornerCoin.position = CGPoint(x: screenWidth / -3.1, y: screenHeight / 3.175) cornerCoin.zPosition = 10 cameraNode.addChild(cor
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)
个人,我不会跟踪屏幕宽度/屏幕高度,它违背了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)
}
我们的标签现在应该在相机的右上角,硬币应该在标签的左侧,与文字垂直居中
如果您不喜欢顶部对齐的外观,您可能需要调整标签的位置。什么是屏幕宽度和屏幕高度?我假设你的场景有一个0,0的主播点?