gpt4 book ai didi

ios - 通用 2D 游戏 Assets 和绝对节点定位

转载 作者:技术小花猫 更新时间:2023-10-29 10:37:25 25 4
gpt4 key购买 nike

我有一个关于 Sprite Kituniversal game assetsabsolute positioning 的问题(iOS 8 岁以上)。

我将尝试通过以下示例来展示我的问题:

想象一个 2D top down 游戏,其中有一个代表房子的 SKSpriteNode。一个房子有多个子 SKSpriteNode,代表椅子、书 table 、沙发等。

我有 3 个版本的房屋 Assets :

  • 1x - 200 x 200px(非视网膜 iPad),
  • 2x - 400 x 400px(视网膜 iPhone 和 iPad),
  • 3x - 600 x 600px (iPhone 6 Plus)。

重要:子节点(椅子、 table 等)的位置在 .plist 文件 中定义。像这样(JSON 表示):

children: [
{
position = {20,20};
},
...
]

由于位置是以点而非像素为单位定义的,因此所有内容都会根据设备屏幕比例按预期定位。对于 1x 设备,位置保持在 {20,20},对于 2x,位置是 {40,40} 并且对于 3x,位置是 {60,60}

问题:

问题是 200x200px400x400px Assets 对于 iPad 设备来说太小了,以便在所有设备上实现相似的外观.

问题:

How to successfully present/import assets in a way that would enable me to achieve similar (if not the same) look and feel on all devices/screen sizes without breaking child nodes positioning?

我的看法:

采取 1:

我可以简单地使用非视网膜 iPad 设备上的现有 400x400px Assets 和视网膜 iPad 设备上的 600x600px Assets 作为房屋节点,但子节点的定位会变得 splinter 。这是因为子位置值不会改变,对于 iPad 设备仍将分别为 {20,20}{40,40},而 Assets 将是大。这会产生相对于房屋节点的不准确的子位置。

拍摄 2:

我还可以缩放 SKScene 大小(缩放效果),同时分别为 iPad 设备使用正常的 200x200px400x400px 大小的资源。这有效并且它使子节点定位工作,但场景/ Assets 的渲染质量不如应有的好。此外,这感觉像是一种黑客攻击,我们不希望这样。

第 3 次:

我还可以为 iPad 设备使用两倍大的 Assets ,并在运行时将子节点位置加倍。在这种情况下,我将为非视网膜 iPad 设备使用 400x400px Assets ,为视网膜 iPad 设备使用新的 800x800px Assets 。虽然这看起来很棒并且可以保持子节点定位正常工作,但它似乎是一个非常大的 hack 在运行时修复子节点位置:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
position.x *= 2.0f;
position.y *= 2.0f;
}

感谢您花时间阅读问题!

最佳答案

I could simply use the existing 400x400px assets on Non-retina iPad devices and 600x600px assets on Retina iPad devices for the house node but the positioning of a child nodes would become broken. This is because the child position value wouldn't change and would still be {20,20} and {40,40} for iPad devices respectively, while the assets would be bigger. This would yield inaccurate child positions relative to the house node.

您可以简单地将房屋节点(而不是场景)缩放到更大的尺寸。您需要做的就是将您房子的比例设置为在更大的设备上看起来不错的值。事实上,我们可以想出一个根据屏幕大小设置比例的公式,而不是检查 iPad。类似下面的代码应该可以工作。请注意,它假设您的房子在 iPhone 4 上完美定位,并且它会始终缩放到所有更大的屏幕。请注意,您确实可以选择任意尺寸作为基本情况,但选择最小的屏幕并按比例放大是最简单的。请务必提供更大的纹理,以便纹理在缩放时不会变得模糊。

[house setScale:self.scene.size.width/320.0];

您可以使用两个节点。用于保存“实际”位置的根节点,然后是用于显示图像的图像节点子节点。这将允许您将位置数据与显示的内容分开。您可以根据需要调整子图像节点的大小和位置,而不会弄乱根节点的实际位置。您甚至可以在 JSON 中包含这个额外的图像节点数据。


I could also scale the SKScene size (zoom effect) while using the normal 200x200px and 400x400px sized assets for iPad devices respectively. This works and it keeps the child nodes positioning working but the rendered quality of the scene/assets is not good as it should be. Also, this feels like a hack and we don't want that.

如果您的应用程序能够以某种方式处理不同的宽高比,那么此选项绝对有效。例如,如果场景的缩放比例大于设备屏幕,您可以允许滚动场景。发生质量损失是因为您将纹理缩放到大于其预期大小。您需要提供更大的纹理以在缩放时保持高质量。在这种情况下,您可能只使用 600x600 图像(或更大)并让它缩放。例如,在我的 OS X 版 RTS Sprite-Kit 游戏中,我缩放了整个场景,以便在所有设备上获得相同的外观。而且我不会损失任何质量,因为我确保提供非常大的纹理,因此在缩放时不会损失质量。


I could also use twice as big assets for iPad devices and double the child nodes position at the runtime. In this case I would use a 400x400px asset for non-retina iPad devices and a new 800x800px asset for retina iPad devices. While this looks great and keeps the child nodes positioning working, it seems like a really big hack fixing child node position during runtime with this:

这也可行,尤其是当您的 iPad 需要自定义布局时。但是,如果可能,请避免专门针对 iPad 检查,而是使用屏幕尺寸来创建布局规则,以便您的节点在所有屏幕尺寸上一致地动态调整(参见上面的代码行)。如果您的 iPad 布局与 iPhone 非常不同,有时这是不可能的,在这种情况下,您别无选择,只能检查 iPad。


这三个解决方案都很好。我不会认为其中任何一个是“hacky”。他们都为不同的目的而工作。您需要找到最适合您的游戏的解决方案。

我还建议您在下面查看我的两个答案。不确定,但它们可以帮助您理解 Sprite Kit 中的通用定位和缩放。

https://stackoverflow.com/a/25256339/2158465

https://stackoverflow.com/a/29171224/2158465

祝你游戏顺利,如果你有任何问题,请告诉我。

关于ios - 通用 2D 游戏 Assets 和绝对节点定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29741710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com