SpriteKit缩放iPad的完整游戏



所以我正在使用SpriteKit在iPhone上开发Xcode的游戏。通过使用node.setscale()方法,它在所有iPhone设备上看起来都一样,并且在所有iPhone设备上都很棒。现在,我也想在iPad上使其通用,并且可以在iPad上运行。我该如何轻松执行此操作?是否有一些公式?

我已经尝试过的是:

if UIDevice.current.userInterfaceIdiom == .pad {
        sscale = frame.size.height / 768
    }else{
        sscale = frame.size.height / 320
    }

,然后对于每个节点,我都会制作node.setscale(sscale)。

这最终不起作用,因为在iPad mini上看起来与iPad Air不同...即节点大小和位置...

有帮助吗?

您基本上有2个选项。

1)将场景大小设置为1024x768(景观)或768x1024(肖像),并使用默认的.ASPECTFILL进行比例模式。这是Xcode 7(iPad分辨率)中的默认设置。

您通常在顶部/底部(景观)或iPad上的左/右(肖像)显示一些额外的背景。

游戏示例在iPad上显示更多:

Altos Adventure,Leos Fortune,Limbo,Line Zen,Modern Combat 5。

2)苹果将Xcode 8中的默认场景大小更改为iPhone 6/7(750*1334-PORTAIT,1337*750-LANDSCAPE)。此设置将在iPad上裁剪您的游戏。

在两个选项之间选择取决于您,取决于您正在制作的游戏。我通常更喜欢使用选项1并在iPad上显示更多背景。

无论场景尺寸规模模式如何,通常最好在.aspectfill或.aspectfit的默认设置下

要调整特定内容,例如设备的标签等,您可以这样做

 if UIDevice.current.userInterfaceIdiom == .pad {
   // adjust some UI for iPads
 }

我不会尝试进行一些随机的黑客,您可以在差异设备上手动更改场景或节点尺寸/缩放,您应该让Xcode/spriteKit为您执行此操作。

希望这有帮助

如果您使用的是SKCameraNode,则可以通过缩放相机来扩展整个场景。

// SKCameraNode+Extensions.swift
extension SKCameraNode {
    func updateScaleFor(userInterfaceIdiom: UIUserInterfaceIdiom) {
        switch userInterfaceIdiom {
            case .phone:
                self.setScale(0.75)
            case .pad:
                self.setScale(1.0)
            default:
                break
        }
    }
}

然后在您的场景初始化时调用它。

guard let camera = self.childNode(withName: "gameCamera") as? SKCameraNode else {
    fatalError("Camera node not loaded")
}
// Update camera scale for device / orientation
let userInterfaceIdiom = UIDevice.current.userInterfaceIdiom 
camera.updateScaleFor(userInterfaceIdiom: userInterfaceIdiom)

最新更新