通用2D游戏资产和绝对节点定位 [英] Universal 2D Game Assets and Absolute Node Positioning

查看:114
本文介绍了通用2D游戏资产和绝对节点定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 SKNodes 通用游戏资产绝对定位的问题> Sprite Kit (iOS 8 +)。

I have a question regarding universal game assets and absolute positioning of a SKNodes in Sprite Kit (iOS 8+).

我将通过以下示例尝试提出我的问题

想象一下 2D自上而下的游戏,其中 SKSpriteNode 代表一所房子。房子有多个孩子 SKSpriteNode s代表椅子,桌子,沙发等。

Imagine a 2D top down game with a SKSpriteNode which represents a house. A house has multiple child SKSpriteNodes which represent chairs, desk, sofa, etc.

我有3个版本的房产资产:

I have 3 versions of house asset:


  • 1x - 200 x 200px (非视网膜iPad),

  • 2x - 400 x 400px (Retina iPhone和iPad),

  • 3x - 600 x 600px (iPhone 6 Plus)。

  • 1x - 200 x 200px (Non-retina iPads),
  • 2x - 400 x 400px (Retina iPhones and iPads),
  • 3x - 600 x 600px (iPhone 6 Plus).

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

Important: Child nodes (chairs, desk, etc.) positions are defined in a .plist file. Something like this (JSON representation):

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

自该职位以来以点而非像素定义,根据设备屏幕比例,所有内容都按预期定位。对于 1x 设备,该位置保持 {20,20} 2x 头寸 {40,40} 3x 头寸 {60 ,60}

Since the position is defined in points and not in pixels, everything gets positioned like expected according to device screen scale. For 1x devices the position stays {20,20}, for 2x position is {40,40} and for 3x the position is {60,60}.

问题

问题是 200x200px 400x400px 资产对于iPad设备来说很小在所有设备上实现类似的外观。

The problem is that 200x200px and 400x400px assets are way to small for iPad devices in order to achieve similar look and feel on all devices.

问题


如何以一种方式成功地呈现/导入资产,使我能够在不破坏子节点的情况下在所有
设备/屏幕尺寸上实现类似(如果不相同)的外观和感觉定位?

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

我可以在非视网膜上使用现有的 400x400px 资产适用于房屋的Retina iPad设备上的iPad设备和 600x600px 资产ode但是子节点的定位会被破坏。这是因为子位置值不会改变,仍然是 {20,20} {40,40} 分别用于iPad设备,而资产则更大。这将导致相对于房屋节点的子位置不准确。

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.

Take 2

我还可以使用普通的 200x200px SKScene 尺寸(缩放效果) > 400x400px 分别用于iPad设备的大小资产。这样可以保持子节点的定位工作,但是场景/资产的渲染质量不是很好。此外,这感觉就像一个黑客,我们不希望这样。

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.

拿3

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

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:

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






谢谢花时间阅读问题!

推荐答案


我可以简单地使用非Retina iPad上的现有400x400px资产
设备和Retina iPad设备上600x600px资产的房屋节点
,但子节点的定位将被打破。这是
,因为儿童位置值不会改变,并且对于iPad设备分别仍然是
{20,20}和{40,40},而资产
会更大。这将产生相对于房屋节点相对
的不准确的子位置。

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上,并且它将一直扩展到所有更大的屏幕。请注意,您确实可以选择任意大小作为基本情况,但选择最小的屏幕并按比例放大是最简单的。请务必提供更大的纹理,以便在缩放时纹理不会变得模糊。

You can simply scale your house node (not the scene) to a larger size. All you need to do is set the scale on your house to a value that looks good on larger devices. And in fact, instead of checking for iPad we can come up with a formula that sets the scale depending on the size of the screen. Something like the code below should work. Note that it assumes your house is positioned perfectly on a iPhone 4 and it will consistently scale to all larger screens. Note that you really could pick any arbitrary size as your base case, but choosing the smallest screen and scaling up is easiest. Just be sure to provide larger textures so that the textures don't become blurry when scaled.

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

OR

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

You could use two nodes. A root node for holding the "actual" position, and then an image node child for displaying the image. This will allow you to separate your positional data from what's being displayed. You could resize and position your child image node however you want without messing with the actual position of the root node. You could even include this extra image node data in your JSON.


我也可以扩展SKScene尺寸(缩放效果),同时为iPad设备使用
普通200x200px和400x400px大小的资产
。这样可以保持子节点定位
,但是场景/资产的渲染质量不好,因为它应该是
。此外,这感觉就像一个黑客,我们不希望这样。

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游戏中,我缩放整个场景,以便在所有设备上获得相同的外观。而且我没有失去任何质量,因为我确保提供非常大的纹理,因此在缩放时不会有质量损失。

This option can definitely work if your App can handle the different aspect ratios in someway. For example you could allow scrolling the scene if the scene is scaled larger than the device screen. The loss in quality occurs because you are scaling the textures larger than their expected size. You need to provide larger textures to keep the quality high when zooming. In this case you could probably just use your 600x600 images (or maybe even larger) and let it scale with zoom. For example, in my RTS Sprite-Kit game for OS X I scale the entire scene so I get the same look across all devices. And I don't lose any quality because I make sure to provide very large textures so there is no loss in quality while scaling.


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

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。

This could also work, especially if your iPad requires custom layout. However, if possible avoid checking specifically for iPad and instead use the screen size to create layout rules so your nodes dynamically adjust on all screen sizes consistently (See the line of code above). Sometimes this is not possible if your iPad layout is very different from the iPhone, in which case you will have no choice but to check for iPad.

这三种解决方案都很好。我不认为他们中的任何一个hacky。它们都可以用于不同的目的。您需要找到最适合 游戏的解决方案。

All three of these solution are good. I wouldn't consider any one of them "hacky." They all work for different purposes. You need to find the solution that works best for your game.

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

I would also recommend you see my two answers below. Not sure but they may help you with understanding universal positioning and scaling in Sprite Kit.

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

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

祝你好运,如果你有任何问题,请告诉我。

Good luck with your game, let me know if you have any questions.

这篇关于通用2D游戏资产和绝对节点定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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