SpriteKit 帮助理解缩放 [英] SpriteKit help understanding scaling
问题描述
使用 SpriteKit 制作仅肖像游戏并将我的图像资产放入 Images.xcassets
.这是针对 iPhone 的,所以我的图像资产设置如下:
Making a portrait only game using SpriteKit and putting my image assets into Images.xcassets
. This is for iPhone, so my image assets are setup like so:
1x, 2x, Retina 4 2x, 3x
例如,在一个图像资产下,一个 2x,我有一个 640x640 的 png,这在 iPhone 4/4s、5/5s 上很合适,但在 6(它也缩放 @ 2x)上就不行,因为 6屏幕稍宽 @ 750px(或 375 点).所以我想出了以下公式,我可以将它传递给 [mySprite setScale:]
用于添加到场景中的每个精灵.
For example, under one image asset, a 2x, I have a 640x640 png, this fits fine on iPhone 4/4s, 5/5s but on 6 (which also scales @ 2x) it doesn't, because the 6 has a slightly wider screen @ 750px (or 375 points). So I came-up with the following formula which I can pass into [mySprite setScale:]
for each and every sprite I add to my scene.
CGFloat scale = screenPointWidth / (screenScale * 160)
其中 screenPointWidth = [[UIScreen mainScreen] bounds].size.width
和 screenScale = [[UIScreen mainScreen] scale]
.160
是指基本的 1x 像素宽度值(或 320/2
).这将返回一个规模多人游戏.
Where screenPointWidth = [[UIScreen mainScreen] bounds].size.width
and screenScale = [[UIScreen mainScreen] scale]
. 160
refers to a base 1x pixel width value (or 320/2
). This returns a scale multiplayer.
通过这个多人游戏应用了我的 640x640 图像比例并完美地适合 750 宽屏幕.
With this multiplayer applied my 640x640 image scales and fits perfectly on a 750 wide screen.
我的问题:有没有更好的方法可以做到这一点,还是我的做法正确?是否有我应该使用的通用缩放设置,或者我应该根据我的公式缩放每个精灵和精灵位置?或者,我应该不使用 Image.xcassets
而是为特定的屏幕分辨率创建单独的 .atlas 吗?我应该关注上采样和下采样,还是由 iPhone 6/6+ 本身自动处理?
My questions: is there a better way to do this, or am I doing it correct? Is there a universal scaling setting I should be using instead, or should I just scale each and every sprite and sprite position based on my formula? Or, should I not be using Image.xcassets
and instead create individual .atlas' for specific screen resolutions? Should I be concerned about upsampling and downsampling, or is that handled auto-magically by the iPhone 6/6+ itself?
参考:http://www.paintcodeapp.com/news/iphone 分辨率终极指南
推荐答案
iPhone 6 的分辨率与 iPhone 5 非常接近,可以使用 2x 图像.
iPhone 6 is close enough in resolution to iPhone 5 that it's meant to use 2x images.
1136 x 640 - iphone5
1136 x 640 - iphone5
1334 x 750 - iphone 6
1334 x 750 - iphone 6
因为您的背景是 640 像素高,所以在纵向模式下它的顶部和底部会缺少一些空间.您需要使用不介意在较小设备(iphone 4 和 5)上被切断的区域制作背景.或者你需要在 iphone 6 上的游戏周围有一个黑框.我不建议缩放每个图像..
Because your background is 640 pixels tall, it will have some space missing on the top and bottom in portrait mode. You need to make a background with areas that you don't mind being cut off on smaller devices (iphone 4 and 5). Or you need to have a black frame around your game on the iphone 6. I wouldn't recommend scaling every image..
即使在 iphone 6 出现之前,这也是一个常见问题.这张图片显示了我在说什么
This was a common issue even before iphone 6 was around. This image kind of shows what I'm talking about
http://cdn5.raywenderlich.com/wp-content/uploads/2013/09/Aspect-Ratio-Diff.jpg
图像尺寸
您需要三种图像尺寸.
@2x = iphone4s, 5, 5s, 6(你也可以将它用于非视网膜 ipad)
@2x = iphone4s, 5, 5s, 6 (you use this for non retina ipads as well)
@3x (1.5 * 2x) = iphone 6 PLUS
@3x (1.5 * 2x) = iphone 6 PLUS
@2x~ipad (2 * 2x) = ipad 视网膜和迷你视网膜
@2x~ipad (2 * 2x) = ipad retina and retina mini
您想以何种方式组织这件事取决于您.在我的游戏中,我将图像分成两组地图集.一种用于 iPad,一种用于 iPhone.
the way you want to organize this is up to you. In my game I split my images up into two sets of atlases. one for iPad and one for Iphone.
我整理地图集的方式
无后缀的图片适用于非视网膜 ipad.
Images with no suffix are for the non-retina ipads.
在我的代码中,我有一个简单的函数来决定我要加载哪个纹理图集.
In my code I have a simple function to decide which texture atlas I want to load.
func getTextureAtlas(filename: String) -> SKTextureAtlas {
var name = filename
if UIDevice.currentDevice().userInterfaceIdiom != .Phone {
name = "\(name)-ipad"
}
return SKTextureAtlas(named: name)
}
示例用法:
shipAtlas = getTextureAtlas("ship")
这篇关于SpriteKit 帮助理解缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!