在SpriteKit中处理不同的iOS设备分辨率 [英] Dealing with different iOS device resolutions in SpriteKit

查看:84
本文介绍了在SpriteKit中处理不同的iOS设备分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode 6,iOS 8 beta 5中使用SpriteKit。所有内容都在iPhone 4S模拟器上完美布局并完美运行,但是当切换到5S时,屏幕底部的元素被切断关闭。

I'm playing around with SpriteKit in Xcode 6, iOS 8 beta 5. Everything is all laid out and working perfectly on the iPhone 4S simulator, however when switching to the 5S, the elements at the bottom of the screen are cut off.

根据我的理解,iPhone屏幕的左下角应该是CGPoint(0,0)但是在通过将坐标打印到控制台来检查位置之后我可以点击左角的最低点是(5,44)。我的场景设置是否有什么问题造成这种情况?

It was to my understanding that the bottom left corner of the iPhone screen should be CGPoint(0, 0) but after checking the location by printing the coordinates to the console that the lowest point of the left corner I could click was around (5, 44). Is there something wrong in my scene setup thats causing this?

没有对GameViewController文件进行任何更改,甚至在我剥离GameScene文件后问题仍然存在。

No changes have been made to the GameViewController file and even after I strip the GameScene file the problem persists.

至少有人能指出我正确的方向吗?

Can anyone at least point me in the right direction with this?

推荐答案

添加以下代码修复您的问题(代码在Swift中):

Adding the following code will fix your problem (code is in Swift):

scene.scaleMode = SKSceneScaleMode.ResizeFill

现在,如果您想知道为什么这可以解决您的问题,那么您的问题实际上是什么,以及如何处理多种分辨率 - 我建议你继续阅读。

Now if you want to know why this fixes your problem, what your problem actually is, and how to handle multiple resolutions – I suggest you continue reading.

有三件事可能会影响场景中节点的位置。

There are three things that can impact the position of nodes in your scene.

1)锚点

确保场景的锚点设置为(0,0)左下角。默认情况下,场景的锚点从(0,0)开始,所以我认为这不会导致问题。

1) Anchor Point
Make sure your scene's anchor point is set to (0,0) bottom left. By default the scene's anchor point starts at (0,0) so i'm assuming that is not causing the issue.

2)尺寸
检查场景的大小。我通常使我的场景大小与设备的大小相匹配(即iPad,iPhone 4英寸,iPhone 3.5英寸),然后我在场景中放置另一层来存储我的节点。这使我能够为分辨率较小的设备执行滚动效果,但这取决于您的游戏当然。我的猜测是你的场景大小可能设置为320,480,这可能会导致iPhone 5s出现定位问题。

2) Size
Check the size of your scene. I typically make my scene size match the size of the device (i.e. iPad, iPhone 4-inch, iPhone 3.5 inch), then I place another layer in the scene for storing my nodes. This makes me able to do a scrolling effect for devices with smaller resolutions, but it depends on your game of-course. My guess is that your scene size might be set to 320, 480 which could be causing the positioning problems on your iPhone 5s.

3)缩放模式
缩放模式对场景中节点的定位有很大影响。确保将比例模式设置为对您的游戏有意义的事物。当场景大小与视图大小不匹配时,缩放模式会启动。所以缩放模式的目的是让Sprite Kit知道如何处理这种情况。我的猜测是你将场景大小设置为320,480并且场景被缩放以匹配iPhone 5视图,这将导致与你描述的定位问题相同的定位问题。以下是您可以为场景设置的各种比例模式。

3) Scale Mode
The scale mode has a huge effect on the positioning of nodes in your scene. Make sure you set the scale mode to something that makes sense for your game. The scale mode kicks in when your scene size does not match the size of the view. So the purpose of the scale mode is to let Sprite Kit know how to deal with this situation. My guess is that you have the scene size set to 320,480 and the scene is being scaled to match the iPhone 5 view which will cause positioning problems identical to what you described. Below are the various scale modes you can set for your scene.

SKSceneScaleMode.AspectFill




计算每个维度的比例因子,并选择两者中较大的
。场景的每个轴都以相同的
比例因子缩放。这可以保证视图的整个区域都是
填充,但可能会导致部分场景被裁剪。

The scaling factor of each dimension is calculated and the larger of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire area of the view is filled, but may cause parts of the scene to be cropped.


SKSceneScaleMode.AspectFit




计算每个维度的比例因子,并选择两个较小的
。场景的每个轴都以相同的
比例因子缩放。这可以保证整个场景可见,但
可能需要在视图中使用letterbox。

The scaling factor of each dimension is calculated and the smaller of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire scene is visible, but may require letterboxing in the view.


SKSceneScaleMode.Fill




场景的每个轴都是缩放,因此场景中
中的每个轴都精确映射到视图中该轴的长度。

Each axis of the scene is scaled independently so that each axis in the scene exactly maps to the length of that axis in the view.


SKSceneScaleMode.ResizeFill




场景未缩放以匹配视图。相反,场景是
自动调整大小,以便其尺寸始终与视图的
匹配。

The scene is not scaled to match the view. Instead, the scene is automatically resized so that its dimensions always matches those of the view.


结论


看起来你想删除你的缩放比例场景,这样你在场景中的位置将匹配视图中的实际位置。您可以设置场景的大小以匹配视图大小,在这种情况下不会进行缩放。或者您可以将场景的缩放模式设置为ResizeFill,这将始终使场景的大小与视图的大小相匹配,并且不会缩放任何内容。一般来说,我会远离任何缩放,而是调整界面和场景大小以最适合每个设备。您可能还想添加缩放和/或滚动以允许具有较小分辨率的设备实现相同的视图字段。

Conclusion
It looks like you want to remove the scaling of your scene, that way your positions in the scene will match the actual positions in the view. You can either set your scene's size to match the view size, in which case no scaling will take place. Or you can set your scene's scale mode to ResizeFill which will always make the scene's size match your view's size and it won't scale anything. In general I would stay away from any scaling and instead adjust the interface and the scene size to best suit each device. You may also want to add zoom and/or scrolling to allow devices with smaller resolutions to achieve the same view field.

但是如果我想缩放我的场景怎么办?

但是如果你需要缩放场景,但你仍然希望位置相对于视图(即你想要的(0) ,0)即使在场景被截止时也是屏幕的左下角)然后看到我的答案这里

其他信息

请参阅答案这里示例代码显示了我如何动态布局节点。

Additional Info
See answer here for sample code showing how I layout nodes dynamically.

参见答案其他有关扩展以支持多个设备的更多详细信息。

See answer here for more details about scaling to support multiple devices.

这篇关于在SpriteKit中处理不同的iOS设备分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆