gpt4 book ai didi

SpriteKit : Sprite z-Order

转载 作者:行者123 更新时间:2023-12-02 23:17:32 25 4
gpt4 key购买 nike

如何在 Sprite-Kit 中更改 Sprite 的 z 顺序?

Cocos2d 代码:

[parentNode addChild:sprite1 z:3];
[parentNode addChild:sprite2 z:2];
[parentNode addChild:sprite3 z:1];

我在 Sprite-Kit 中尝试了以下代码..但它没有改变 z 顺序

[parentNode insertChild:sprite1 atIndex:3];
[parentNode insertChild:sprite2 atIndex:2];

最佳答案

对于抽签顺序,请使用 zPosition节点的属性:

zPosition The height of the node relative to its parent.

The default value is 0.0. The positive z axis is projected toward the viewer so that nodes with larger z values are closer to the viewer. When a node tree is rendered, the height of each node (in absolute coordinates) is calculated and then all nodes in the tree are rendered from smallest z value to largest z value. If multiple nodes share the same z position, those nodes are sorted so that parent nodes are drawn before their children, and siblings are rendered in the order that they appear in their parent’s children array. Hit-testing is processed in the opposite order.

The SKView class’s ignoresSiblingOrder property controls whether node sorting is enabled for nodes at the same z position.

您还可以使用 insertChild:atIndex: 来影响 z 顺序,但请记住,index 是数组中对象的索引,而不是 zPosition (即atIndex 参数与 cocos2d 中的 z 参数不一样,实际上是相反的)。在较低索引处添加的节点将在较高索引处的节点之前绘制。

另请注意,您不能在越界索引处插入节点。来自 NSMutableArray docs:

[..] you cannot insert an object at an index greater than the current count of an array. For example, if an array contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds; if you try to add an object at index 3 (when the size of the array is 2), NSMutableArray raises an exception.

关于 SpriteKit : Sprite z-Order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22304807/

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