在SpriteKit中,UIScreen的界限并不准确

时间:2017-04-06 03:40:44

标签: ios swift xcode sprite-kit

我正在做一个管道上下移动的游戏,我需要知道屏幕的高度,所以我知道管道何时应该改变方向。这是管道的起始位置:

topPipe.position.y = UIScreen.main.nativeBounds.size.height + topPipe.size.height/2
bottomPipe.position.y = UIScreen.main.nativeBounds.size.height - bottomPipe.size.height/2 - emptyGap

我知道如何做到这一切并且管道成功上下移动,它在我的iPhone 7+上运行良好。但是,当我在iPhone 5模拟器上运行它时,管道只会在返回屏幕的一半左右。似乎UIScreen.main.nativeBounds.size.height没有返回它正在运行的屏幕的实际高度。所以,我的应用程序与7+以外的任何iPhone都不兼容,因为我手动将gameScene.sks中的屏幕尺寸设置为1080×1920。还有其他方法可以找到屏幕的实际高度吗?

如果你想看看我是如何移动管道的(屏幕的锚点位于左下角,所以我可以添加屏幕高度,知道屏幕顶部的位置,这很好知道0是屏幕的最左侧,因为管道保持在左侧):

//moves pipes
if topPipe.position.y - topPipe.size.height/2 >= UIScreen.main.nativeBounds.size.height - 1{

bottomPipe.run(SKAction.move(to: CGPoint(x: bottomPipe.position.x, y: 0 - bottomPipe.size.height/2), duration: movementDuration))

topPipe.run(SKAction.move(to: CGPoint(x: bottomPipe.position.x, y: 0 + topPipe.size.height/2 + emptyGap), duration: movementDuration))

    } else if bottomPipe.position.y <= 0 - bottomPipe.size.height/2 + 1{

bottomPipe.run(SKAction.move(to: CGPoint(x: bottomPipe.position.x, y: UIScreen.main.nativeBounds.size.height - bottomPipe.size.height/2 - emptyGap), duration: movementDuration))

topPipe.run(SKAction.move(to: CGPoint(x: bottomPipe.position.x, y: UIScreen.main.nativeBounds.size.height + topPipe.size.height/2), duration: movementDuration))

    }

1 个答案:

答案 0 :(得分:1)

要求bounds而不是nativeBounds。想一想,而不是像素。