使spritekit中的SKLabelNode显示在所有其他sprite之上

时间:2022-07-03 15:53:31

Hi im pretty new to programming and I have encountered a small issue. I am creating a simple game which has a score label. However, the score label is covered whenever a downward scrolling obstacle passes. I would like for the score label to not be covered by the obstacles.Is there a way i can change the sprite hierarchy in spritekit? Thanks

嗨我很新编程,我遇到了一个小问题。我正在创建一个带有分数标签的简单游戏。但是,只要向下滚动障碍物通过,就会覆盖乐谱标签。我希望得分标签不被障碍覆盖。有没有办法可以改变spritekit中的sprite层次结构?谢谢

3 个解决方案

#1


2  

I would recommend creating a node for the static stuff that should be fixed on screen (like a game HUD). Then you set this node's zPosition to be higher then the game play node (where all game objects are placed and manipulated).

我建议为应该在屏幕上修复的静态内容创建一个节点(比如游戏HUD)。然后将此节点的zPosition设置为高于游戏节点(放置和操纵所有游戏对象的位置)。

This makes it also easier implementing a camera or moving the game play node if needed without the score label or any other fixed hud element moving as well.

这使得在没有得分标签或任何其他固定的hud元素移动的情况下实现相机或移动游戏节点也更容易。

So you will have something like this :

所以你会有这样的事情:

SKNode *hudNode = [SKNode node];
hudNode.zPosition = 1000;
[hudNode addChild:scoreLabel];

SKNode *gamePlayNode = [SKNode node];
gamePlayNode.zPosition = 0; // Not mandatory as it defaults to 0 but I put this here for clarification

[self addChild:hudNode]; // self is your scene instance
[self addChild:gamePlayNode]; 

#2


2  

You can change the zPosition property of your label node, by making it 100 it will be above all other nodes (default value is 0.0):

您可以更改标签节点的zPosition属性,使其为100,它将高于所有其他节点(默认值为0.0):

labelNode.zPosition = 100;

#3


2  

Nodes are drawn in the order they are added to the scene (or parent node). You can change the order in which nodes are drawn by setting their zPosition property.

节点按照它们添加到场景(或父节点)的顺序绘制。您可以通过设置zPosition属性来更改节点的绘制顺序。

From Apple's documentation...

来自Apple的文档......

...The z position is the node’s height relative to its parent node, much as a node’s position property represents its x and y position relative to parent’s position. So you use the z position to place a node above or below the parent’s position.

... z位置是节点相对于其父节点的高度,就像节点的position属性表示相对于父节点的x和y位置一样。因此,您使用z位置将节点放在父节点的上方或下方。

When you take z positions into account, here is how the node tree is rendered:

考虑z位置时,以下是节点树的呈现方式:

  • Each node’s global z position is calculated.
  • 计算每个节点的全局z位置。
  • Nodes are drawn in order from smallest z value to largest z value.
  • 节点按从最小z值到最大z值的顺序绘制。
  • If two nodes share the same z value, ancestors are rendered first, and siblings are rendered in child order.
  • 如果两个节点共享相同的z值,则首先呈现祖先,并按子顺序呈现兄弟节点。

You can also use zPosition to optimize rendering performance...

您还可以使用zPosition优化渲染性能......

...it might be better if Sprite Kit could gather all of the nodes that share the same texture and drawing mode and and draw them with a single drawing pass. To enable these sorts of optimizations, you set the view’s ignoresSiblingOrder property to YES.

...如果Sprite Kit可以收集共享相同纹理和绘图模式的所有节点并使用单个绘图过程绘制它们可能会更好。要启用这些类型的优化,请将视图的ignoresSiblingOrder属性设置为YES。

#1


2  

I would recommend creating a node for the static stuff that should be fixed on screen (like a game HUD). Then you set this node's zPosition to be higher then the game play node (where all game objects are placed and manipulated).

我建议为应该在屏幕上修复的静态内容创建一个节点(比如游戏HUD)。然后将此节点的zPosition设置为高于游戏节点(放置和操纵所有游戏对象的位置)。

This makes it also easier implementing a camera or moving the game play node if needed without the score label or any other fixed hud element moving as well.

这使得在没有得分标签或任何其他固定的hud元素移动的情况下实现相机或移动游戏节点也更容易。

So you will have something like this :

所以你会有这样的事情:

SKNode *hudNode = [SKNode node];
hudNode.zPosition = 1000;
[hudNode addChild:scoreLabel];

SKNode *gamePlayNode = [SKNode node];
gamePlayNode.zPosition = 0; // Not mandatory as it defaults to 0 but I put this here for clarification

[self addChild:hudNode]; // self is your scene instance
[self addChild:gamePlayNode]; 

#2


2  

You can change the zPosition property of your label node, by making it 100 it will be above all other nodes (default value is 0.0):

您可以更改标签节点的zPosition属性,使其为100,它将高于所有其他节点(默认值为0.0):

labelNode.zPosition = 100;

#3


2  

Nodes are drawn in the order they are added to the scene (or parent node). You can change the order in which nodes are drawn by setting their zPosition property.

节点按照它们添加到场景(或父节点)的顺序绘制。您可以通过设置zPosition属性来更改节点的绘制顺序。

From Apple's documentation...

来自Apple的文档......

...The z position is the node’s height relative to its parent node, much as a node’s position property represents its x and y position relative to parent’s position. So you use the z position to place a node above or below the parent’s position.

... z位置是节点相对于其父节点的高度,就像节点的position属性表示相对于父节点的x和y位置一样。因此,您使用z位置将节点放在父节点的上方或下方。

When you take z positions into account, here is how the node tree is rendered:

考虑z位置时,以下是节点树的呈现方式:

  • Each node’s global z position is calculated.
  • 计算每个节点的全局z位置。
  • Nodes are drawn in order from smallest z value to largest z value.
  • 节点按从最小z值到最大z值的顺序绘制。
  • If two nodes share the same z value, ancestors are rendered first, and siblings are rendered in child order.
  • 如果两个节点共享相同的z值,则首先呈现祖先,并按子顺序呈现兄弟节点。

You can also use zPosition to optimize rendering performance...

您还可以使用zPosition优化渲染性能......

...it might be better if Sprite Kit could gather all of the nodes that share the same texture and drawing mode and and draw them with a single drawing pass. To enable these sorts of optimizations, you set the view’s ignoresSiblingOrder property to YES.

...如果Sprite Kit可以收集共享相同纹理和绘图模式的所有节点并使用单个绘图过程绘制它们可能会更好。要启用这些类型的优化,请将视图的ignoresSiblingOrder属性设置为YES。