【发布时间】:2016-07-27 20:44:34
【问题描述】:
我正在创建我的第一个 Swift + SpriteKit 游戏,并且我正在处理必须根据 iDevice 调整我所有东西的大小的问题。
我有一个类,它基于它运行的设备,返回它的名称。 有了这个名称,我创建了一个基于设备字符串名称的字典,返回给定设备的分辨率。
然后我创建了 2 个函数 -> getSizeBasedOnDevice() 和 getPositionBasedOnDevice()
自从我为 iPhone 6S 编写了我的游戏。我以为只要做一个简单的数学计算就可以得到所有这些尺寸和位置,但看起来并非如此。
这些是我的功能:
func getSizeBasedOnDevice(width: CGFloat, height: CGFloat) -> CGSize {
// iPhone 6s w: 375, h: 667
let deviceName = UIDevice.currentDevice().modelName
let iphoneNative = ScreenSize(width: 375, height: 667)
let resolution = Resolutions[deviceName]
let newWidth = (CGFloat(resolution!.getWidth()) * width ) / iphoneNative.getWidth()
let newHeight = (CGFloat(resolution!.getHeight()) * height) / iphoneNative.getHeight()
return CGSize(width: newWidth, height: newHeight)
}
func getPositionBasedOnDevice(x: CGFloat, y: CGFloat) -> CGPoint {
let deviceName = UIDevice.currentDevice().modelName
let iphoneNative = ScreenSize(width: 375, height: 667)
let resolution = Resolutions[deviceName]
let newX = (CGFloat(resolution!.getWidth()) * x) / iphoneNative.getWidth()
let newY = (CGFloat(resolution!.getHeight()) * y) / iphoneNative.getHeight()
return CGPoint(x: newX, y: newY)
}
我的 iPhoneNative 高度和宽度将是我的 iPhone 6S'。 所以计算对我来说有点简单,但事实并非如此。
所以有这些示例值:
Asset's width = 100 pts
Current Device (iPhone 4S) width = 320 pts
Originally designed Device (iPhone 6S) width = 375 pts
数学是:
(320 * 100) / 375 = 85.33
因此,在我的 iPhone 6S 上宽度为 100 磅的资产在 iPhone 4S 上的宽度为 85.33。
起初我认为这似乎很好,但看看结果,我发现我做错了。
谁能给我一个提示?
提前致谢。
【问题讨论】:
标签: ios iphone swift sprite-kit resolution