Layer
Last updated
Was this helpful?
Last updated
Was this helpful?
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 .
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.
There are two ways to remove objects from Layers:
Completely remove the object from the engine and its Layer(s)
Remove the object just from the Layer but keep it in the engine
For reference, let's use the following setup to discuss this point:
This is the most common method of removing an object. It's usually what you'll do if you're manually handling the removal of your objects (as opposed to letting Glue do it for you). To do this, simply use the manager removal methods:
In other words, remove your objects just like normal - they'll automatically get removed from their Layers too.
Objects can be removed from Layers through the Remove method.
Multiple layers can exist at a given time. Calling AddLayer multiple times creates multiple layers. The newest layer is always on top while the oldest is on bottom.
In code, layers are created through the . The following code creates a layer and two . Although the named nearSprite is closer than the named farSprite, farSprite is not hidden by nearSprite.
- Responsible for adding SpriteFrames, Sprites, and IDrawableBatches to Layers.
- Responsible for adding Texts to Layers.
Now the and object are no longer on the layer, so they will not be drawn. However, the two objects have not been removed from their respective managers so they are still in memory and still managed every frame!
This section discusses sorting objects (such as within a Layer. For information on how to control the order that Layers are drawn, see . While Layers represent Lists in some ways, they cannot sort objects that they hold (like or ). The reason for this is because the internal that a Layer references is used directly by the engine to draw the Layer. The engine may perform sorting on this list to ensure proper overlapping and to improve runtime performance so user-controlled sorting of the SpriteList may disrupt this behavior. If objects in a Layer need to be sorted they should be added to a separate and sorted there.
See .
If you are using then you can add an Entity to a Layer. See for information on this.
Layers will render to the full screen by default. Layers can be adjusted to only render to part of the Screen. For more information see .
For more information, see the page.