【问题标题】:How can I put a coin label in the corner of every device in my universal app?如何在我的通用应用程序中的每个设备的角落放置硬币标签?
【发布时间】:2017-07-29 14:26:25
【问题描述】:

我想在每个设备的左上角创建一个 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,0?

标签: ios swift sprite-kit sklabelnode


【解决方案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)
}

我们的标签现在应该在我们相机的右上角,硬币应该在我们标签的左边,与文本垂直居中。

如果您不喜欢顶部对齐的外观,您可能想尝试一下标签的定位方式。

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    相关资源
    最近更新 更多