githubEdit

Layer Membership Details

Introduction

Layers provide a way to force a particular order of drawing on a single Cameraarrow-up-right. While the concept is fairly straight-forward, things can become more complicated when objects are moved from being layered to unlayered, or when objects have presence on multiple layers. Since there are a variety of ways to layer and unlayer objects, this article will present a number of small code blocks and explain what each one does.

The setup

First, let's start with some common code that all samples below will use. Consider the following line of code:

Layer firstLayer = SpriteManager.AddLayer();
Layer secondLayer = SpriteManager.AddLayer();

Okay, so far we have two layers that have been added to the SpriteManager. Keep in mind that secondLayer will actually be drawn on top of firstLayer - they are drawn in the order that they are created, so first is drawn first, then second is drawn on top of first.

A simple unlayered Sprite

Here's a very common setup (of course, assuming the lines above are still in the code).

Sprite sprite = SpriteManager.AddSprite("redball.bmp");

As you may have guessed, the above code creates a Sprite which is managed and drawn, but not layered. We can show the membership as follows:

Category

Is a member

Managed by SpriteManager

X

Drawn unlayered

X

Drawn on firstLayer

Drawn on secondLayer

This table is just another way of showing what we mentioned before.

Making a layered Sprite

There are two ways to make a layered Spritearrow-up-right. The first is to use the AddToLayer method:

The AddToLayer method in the SpriteManager as well as all other Managers does two things:

  1. Adds the Sprite (or other object) to the argument Layer

  2. Removes the Sprite (or other object) from unlayered drawing

The result:

Category

Is a member

Managed by SpriteManager

X

Drawn unlayered

Drawn on firstLayer

X

Drawn on secondLayer

The SpriteManager provides a shortcut method for adding a Spritearrow-up-right directly to a Layer:

This code does the exact same thing if used instead of the two lines of code above. That is, this method both adds the Sprite for membership as well as adds it to the firstLayer but not to unlayered drawing.

Category

Is a member

Managed by SpriteManager

X

Drawn unlayered

Drawn on firstLayer

X

Drawn on secondLayer

Multiple Layer membership

The AddToLayer method does remove the argument Spritearrow-up-right from unlayered drawing, but it does not remove the argument Spritearrow-up-right from any other Layers that it belongs to. Therefore, a Spritearrow-up-right can be added to two Layers simply by calling AddToLayer twice:

The result:

Category

Is a member

Managed by SpriteManager

X

Drawn unlayered

Drawn on firstLayer

X

Drawn on secondLayer

X

Removal from Layers

To remove a Spritearrow-up-right (or any other object) from a Layer, simply call the Remove method. Keep in mind that calling Remove will not re-add the object to unlayered drawing.

As shown below, this makes the Spritearrow-up-right managed, but it will not be drawn.

Category

Is a member

Managed by SpriteManager

X

Drawn unlayered

Drawn on firstLayer

Drawn on secondLayer

Layered and Unlayered Drawing

Having an object be drawn layered an unlayered is not something supported by the FlatRedBall Engine. One reason for this is that in most cases, this is an undesirable behavior. It can hurt performance, as well as result in unexpected graphical behavior. But we're going to cover this anyway because it may help expose bugs that you are experiencing if you've tried this (or stumbled across it unintentionally).

This code needs some explanation. The first line makes the Spritearrow-up-right managed and drawn unlayered. The second line makes the Spritearrow-up-right layered, but removes it from unlayered drawing. Now, you may be thinking "Ok, the last line just re-adds the Sprite to the SpriteManager so it is drawn unlayered." Well, you're half right. The second AddSprite call does add the Spritearrow-up-right to be drawn, but it also has a nasty side-effect. The Spritearrow-up-right will actually be added to the SpriteManagerarrow-up-right twice. It was added once on the first line, and once again on the third line. This means that the Spritearrow-up-right will have its every-frame management performed twice. Not only does this hurt performance, but it results in properties like Velocity and Acceleration applied twice. In other words, you may get very unexpected behavior if you do this.

Category

Is a member

Managed by SpriteManager

X X

Drawn unlayered

X

Drawn on firstLayer

X

Drawn on secondLayer

The AddToLayer behavior

The AddToLayer method has a little bit of inconsistent behavior. Let's explore it a little bit. FlatRedBall assumes that in most cases whenever you want an object drawn, you also want it managed. So, let's look at the following code:

So it's a safe bet that the Spritearrow-up-right will be drawn on the firstLayer, but what about management? Is it managed by the SpriteManagerarrow-up-right? We never called AddSprite. Drum roll please...

Category

Is a member

Managed by SpriteManager

X

Drawn unlayered

Drawn on firstLayer

X

Drawn on secondLayer

The SpriteManagerarrow-up-right actually added the object both to the layer as well as to itself for management. This behavior is present because it's assumed that drawn objects should be managed. There is one exception, however. The ShapeManagerarrow-up-right's AddToLayer method. Shapes have somewhat unique behavior in that they are most often used for collision, and many times they can exist and be used, but not be managed by the ShapeManagerarrow-up-right for performance reasons. Therefore, the ShapeManagerarrow-up-right does not add a shape for management unless you explicitly tell it to do so. In other words, adding a shape to a layer, but not to the ShapeManagerarrow-up-right results in the shape being drawn, but not managed. For more information on the reasoning behind this type of behavior, see the "Why Limit Shape Management"arrow-up-right section of the ShapeManagerarrow-up-right page.

Last updated

Was this helpful?