Layer
Last updated
Last updated
Layers can be used to control the order of visual objects on screen. Typically objects with a larger Z value appear on top of objects with smaller Z values, but this relationship is only true if the objects are on the same layer.
Layers take priority over the Z value of objects when performing rendering. Typically Layers are used to force groups of visual objects to draw on top of other objects. For example, a HUD layer can be used to force HUD UI such as score and health bars to appear on top of everything else in a game regardless of Z value.
Layers can be added in the FlatRedBall Editor or in code through the SpriteManager. For information on how to use Layers in the FRB Editor, see this article.
To create a new Layer:
Expand the Screen which should contain a layer. Note that layers are usually added to GameScreen and not Level screens unless you need to have a layer specific to a level.
Right-click on the Objects folder and select Add Object
Find the Layer type
Enter a name for the Layer
Click OK
The following types can be added to Layers:
Additionally, Entities can be added to shapes through the FlatRedBall Editor, or through their MoveToLayer method.
Layers are used only to control the drawing order of objects. This means that layers have nothing to do with the position of the objects that they contain. For example, objects in an entity which are attached to the root entity can span multiple layers despite having their positions controlled by the parent/child relationship. For example we can consider a typical entity which is made up of three (3) objects:
The Entity itself
The visible representation which is attached to the Entity, such as a Sprite
The collision object which is also attached to the Entity, such as an AxisAlignedRectangle
Of these three, only the visible representation needs to be layered. You can add the collision to a Layer, but this does not have any impact on the behavior of your collision. The only reason you might want to add the collision object to a layer is so it will be drawn if on the same layer as the visible representation if you desire to have it drawn for debugging reasons. To clarify, adding the collision to a Layer does not impact collision. Two entities on different Layers will still be able to have their collision objects collide. The collision objects do not consider Layers when performing collision.
In code, layers are created through the SpriteManager. The following code creates a layer and two Sprites. Although the Sprite named nearSprite is closer than the Sprite named farSprite, farSprite is not hidden by nearSprite.
SpriteManager.AddToLayer - Responsible for adding SpriteFrames, Sprites, and IDrawableBatches to Layers.
TextManager.AddToLayer - Responsible for adding Texts to Layers.