【问题标题】:How to change the size of sprite nodes to fit different screens in sprite kit (Swift)如何更改精灵节点的大小以适应精灵套件(Swift)中的不同屏幕
【发布时间】:2016-07-18 15:21:35
【问题描述】:

我正在创建一个游戏,我几乎完成了,但是我遇到的一个烦人的问题是我的游戏不适合 iPhone 5 的屏幕。我使用了尺寸等级,将比例设置为方面填充,甚至尝试调整大小填充,但它不适用于 iPhone 5。它适用于所有其他屏幕尺寸(iPhone 4s 除外,因为我不支持它),包括平板电脑。除了尺寸等级,还有其他选择吗?我必须以编程方式更改图像吗?如果是这样,我将如何在 sprite kit 中做到这一点?

【问题讨论】:

  • 这没有意义,你有 3 个案例 4:3(iPad)、16:9(>=iPhone 5) 和 3:2(iphone 4s) 如果它不适合其中任何一个3,那么它根本不适合任何东西。它不适合的唯一方法是,如果您使用调整大小填充,并围绕更大的屏幕尺寸开发坐标,但如果是这种情况,那么您的所有设备的布局都会不同
  • 你不应该处理尺寸类,这是为 UIViews 保留的,所以除非你的精灵场景是一个小窗口,否则将其保留为默认值
  • 你用的那三种情况是什么?
  • 这些是 ios 设备上可用的方面
  • 我很困惑,所以我应该使用通用吗?我最初开发了适合 iPhone 6 的图像。那么,如果我不使用尺寸等级,我将如何使我的图像适合其他屏幕尺寸?

标签: ios swift sprite-kit


【解决方案1】:

您可以做到这一点的一种方法是检测用户正在使用什么设备,然后更改变量以适应该设备。

这是一个使用 UIDevice() 的扩展,您可以检测用户使用的设备:

public extension UIDevice {

var modelName: String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8 where value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }

    switch identifier {
    case "iPod5,1":                                 return "iPod Touch 5"
    case "iPod7,1":                                 return "iPod Touch 6"
    case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
    case "iPhone4,1":                               return "iPhone 4s"
    case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
    case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
    case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
    case "iPhone7,2":                               return "iPhone 6"
    case "iPhone7,1":                               return "iPhone 6 Plus"
    case "iPhone8,1":                               return "iPhone 6s"
    case "iPhone8,2":                               return "iPhone 6s Plus"
    case "iPhone8,4":                               return "iPhone SE"
    case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
    case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
    case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
    case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
    case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
    case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
    case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
    case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
    case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
    case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
    case "AppleTV5,3":                              return "Apple TV"
    case "i386", "x86_64":                          return "Simulator"
    default:                                        return identifier
    }
}

那么你可以这样使用它

if UIDevice.modelName == "iPhone 4" {
    theNode.xScale = whatever
    theNode.yScale = whatever
}

【讨论】:

  • 如果这是正确答案,请将其标记为正确答案,以便其他有此问题的人得到答案
猜你喜欢
  • 2015-03-31
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多