- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Swift 编程新手。我正在尝试创建一个游戏,其中有一堆在屏幕上随机移动的 Sprite 。假设我有一堆简单的 2D 图像 Sprite 。当 Sprite 向上移动时(如 y 位置增加),我希望它看起来更向后移动到其他 Sprite 后面,所以我希望 zPosition 与其他 Sprite 相比减少。游戏如何处理这个问题?如果屏幕上有 100 个随机移动的 Sprite 怎么办?我什至不知道如何处理此问题,因此我们将不胜感激。
第二个更先进的概念。因此,假设每个 Sprite 都有多层子 Sprite ,例如 ARM 、腿、衣服、武器等。(我的 Sprite 是高度可定制的。)然后每个 Sprite 随机走动,并且当它们移动到其他 Sprite 后面时需要出现在正确的 zPosition 中 Sprite 。我如何确保所有的 ARM 、腿、衣服和其他 Sprite 层正确显示在每个其他 Sprite 的前面或后面?如果我有一个包含 20 层子 Sprite 的 Sprite ,并且它需要在具有 20 个子 Sprite 的其他 Sprite 的前面和后面无缝移动,该怎么办?这可能是错误的方向,但我可以以某种方式将一个 Sprite 及其子图层设置为 1 层(或者将所有左臂 Sprite 设置为 1 层,将右臂 Sprite 设置为另一层)吗?我看到一些游戏可以处理这个问题,但是处理这个问题背后的概念是什么? (我看到的一款符合我要求的游戏是《辐射避难所》,其中每个人都有 ARM 、腿、武器等,他们走在其他人的前面或后面,所有 Sprite 都以正确的方式出现,但与《辐射》不同Shelter 我的游戏有完全平坦的 2D 图像,这就是我希望它保持的样子。)
第三个更先进的概念。如果我希望这些 Sprite 像一个 Sprite 的 ARM 捕获另一个 Sprite 一样进行交互(因此一只 ARM 将位于第二个 Sprite 的后面,但另一只 ARM 将完全位于另一个 Sprite 的前面),该怎么办?如何处理图层以使其出现在正确的 zPosition 中?也许这是不可能的?好吧,我完成了。很抱歉在一篇文章中提出了一百万个问题。我正在寻找这些问题背后的概念,以便我可以开始规划和编程。谢谢你的帮助。
最佳答案
1) As a sprite move upwards (as in y position is increasing) I want it to appear to move further back behind other sprites, so I want the zPosition to decrease in comparison to the other sprites. How do games handle this?
这可以简单地通过创建一个自定义类(我不建议扩展它)并将 zPosition 覆盖为 Y 的倒数来完成
EG
class Player : SKSpriteNode
{
override var zPosition : CGFloat
{
get
{
//Yes it looks weird to be setting zPosition on get, but we want to ensure the variable storing zPosition is also changed in case any property reads it
super.zPosition = -super.position.y
return super.zPosition
}
set
{
//do nothing
}
}
}
2)What if I have 100 sprites on the screen that are all moving around randomly?
覆盖此设置将处理屏幕上的每个 Sprite (在我的示例中为“玩家”类型),y 位置决定 zPosition
3)How would I make sure all the arms, legs, clothes, and other layers of sprites appear correctly in front or behind every other sprite? Now this gets a little tricky. zPosition is relative to its parent, so you would need to account for how many layers deep your sprite can get zPosition wise, and plan accordingly. Say we have a sprite with shirt pants and a belt.
如果我们像这样构造 Sprite :
body
->衬衫
->裤子
->腰带
然后我们可以将这些子节点的所有 zPositions 设置为 0,因为渲染顺序将使用 zPosition 后跟树顺序,并且不会发生任何变化:
但是如果我们这样构造它: body
->衬衫
->裤子
--->腰带
并且我们想要产生衬衫位于裤子和腰带之间的效果,那么我们需要通过将衬衫和腰带的 zPosition 设置为 1 来进行补偿。
4)What if I have a sprite with 20 layers of sprite children and it needs to move seamlessly in front and behind other sprites with 20 sprite children? Now we have a problem with battling other sprites, so we need to apply some math to our zPosition on the Player to fix it. class Player : SKSpriteNode {
override var zPosition : CGFloat
{
get
{
//Yes it looks weird to be setting zPosition on get, but we want to ensure the variable storing zPosition is also changed in case any property reads it
super.zPosition = -super.position.y * 2 //(We need at least 2 zPosition slots reserved for every sprite)
return super.zPosition
}
set
{
//do nothing
}
}
}
5)But can I somehow make one sprite and its children layers into 1 layer (or make all the left arm sprites into 1 layer and the right arm sprites into another layer)? 6) I see games that handle this, but what's the concept behind how this is handled? Yes, you can use
view.texture(from:)
to convert any node to a texture, then create a new sprite out of the texture.7)What if I wanted these sprites to interact like one sprite's arms grab around another sprite (so one arm will be behind a second sprite person, but the opposite arm will be completely in front of the other sprite person 8)How are the layers handled to appear in the correct zPosition 9)Maybe this is not possible?
这部分现在变得棘手,我们需要改变每个节点的 zPosition,而不是进行乘法。
我们还需要计算每个 Sprite 的绝对位置,以了解我们在 y 轴上的位置
为了做到这一点,我们至少有 3 个选择,
a)我们可以为场景中放置的每个节点创建自定义协议(protocol)+自定义类b) 可以为我们放置在场景上的节点类型(例如 SKSpriteNode 和 SKShapeNode)编写扩展,但 SKNode 除外,因为它是基本节点类c) 我们可以做一些称为“键值观察”的事情,并且我们需要监听 Sprite 上的位置
何时发生变化。我们需要做 KVO 的原因是因为你的 Sprite 可能是在有 SKNode 的地方构建的,而我们无法覆盖它的位置。
所有这三种方法都需要大量的工作,并且需要针对您的游戏环境进行独特的优化才能有效运行,我不会详细写如何做到这一点。
关于swift - 当多个 Sprite 相互移动时,如何处理它们的 zPositions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46967544/
假设我有节点 A、B 和 C。我如何将节点 A 设置为比节点 B 更高的 zPosition,将节点 B 设置为比 C 更高的 zPosition,但将节点 A 设置为比节点 C 更低的 zPosit
我不明白如何更改 View 的 zPosition,我尝试这样做,但没有任何反应: - (void)viewDidLoad { [super viewDidLoad]; UIView
我在控制节点的 zPosition 时遇到问题。如果在场景编辑器上创建,我没有问题,但稍后当我想用代码添加新节点时,zPosition 不受尊重。 //GameScene override func
我想在GameScene.sks中为场景制作背景。我在那里有Tile Map Node,但是当我在GameScene.swift中设置背景时,图片位于我的纹理上。我希望它出现在场景的后面。 这是Gam
我有两个 SKLabelNode,一个显示分数,另一个充当分数后面的阴影。每次我的球员通过一个目标时,我都会通过调用下面的 updateScoreLabels() 方法更新得分标签以增加 1 分。 但
三层被赋予相同的父级,但要添加的第一层上的 zPosition 值没有影响 - 它保留在后面。为什么是这样?我正在阅读的书说子层数组中的位置可以用 zPosition 值覆盖。 import UIKi
我一直认为 SKNode 的 zPosition 是相对于其父节点的,但现在我遇到了相反的效果。 我的场景中有两个父 SKNode,它们的 zPosition 分别为 1 (node1) 和 2 (n
addChild 和 zPosition 对场景有什么影响? 我的意思是:假设我这样做 sprite1.zPosition = 50; sprite2.zPosition = 10; sprite3.
我有一个相当复杂的 UIImageView 层次结构。最初,当一个 UIImageView 移动到另一个 UIImageView 前面时,我小心地交换了同级顺序。然后我发现了 myUIImageVie
我有以下问题:我想要常量,在 parentView.subviews 数组中定义 subview 的顺序。此常量不一定是 subview 数组的索引,但它们可以。 像这样: #define VIEW1
我想将图像放置在导航栏上,但部分放置在导航栏上,部分放置在内容 View 上。所以我将 UINavigationBar.layer.zPostion 设置为 -1,它工作得非常好,尤其是在 iPhon
我有一个 UISearchBar正在努力动态显示列表中的匹配结果,但在试图停止 searchResultsTableView 时遇到了噩梦从模糊结果UIView ,当结果被点击时,它从右边开始动画。
我正在尝试制作一个抽屉,当它被点击时,它会从该行下方滑出。除了无法设置 zposition 之外,我已经完成了所有工作。我尝试了几种不同的方法,但它对我不起作用。我可以将 View 与行的底部对齐,但
我想更改我的瓦片 map 中某些瓦片的 zPosition。我使用以下函数来实现这一点: static func setZPosition(to tileMap: SKTileMapNode, an
我正试图暂停我游戏中的许多 Sprite 。有没有一种方法可以暂停具有给定 zPosition 的 Sprite 或低于给定 zPosition 的所有 Sprite ?这些 Sprite 也可能在更
我在 SpriteKit 场景中遇到了节点 zPosition 的问题。 我正在构建一个管理器节点,它支持对节点 zPosition 进行“类似层”的管理,本质上使您能够摆脱 zPosition 属性
我遇到了一个问题,当值变大时,Core Animation 似乎忽略了我的 zPosition 值。 例子 这很容易复制。 CGFloat largeZPosition = 1000000000; C
我是 Swift 编程新手。我正在尝试创建一个游戏,其中有一堆在屏幕上随机移动的 Sprite 。假设我有一堆简单的 2D 图像 Sprite 。当 Sprite 向上移动时(如 y 位置增加),我希
我有一些 UIViews 位于不同的 zPositions。这个想法是在呈现观点的那一刻有一种深度的感觉。所有 View 的图层都是 MainView.layer 的子图层。我想做的是在我的 View
在 Swift 中,我将尝试在顶部设置引脚位置 (zPosition),我在 map 上添加 10 个引脚,当前引脚(任何一个引脚)显示在顶部并且这些当前引脚图像发生变化,但它没有显示在顶部,请帮助我
我是一名优秀的程序员,十分优秀!