The AddToLayer object adds an existing object instance to the argument FlatRedBall.Graphics.Layer. The first argument object may or may not already be added to the SpriteManager. If the object is an unlayered object then it will be removed from the "world layer" and added to the argument Layer. However, if an object is already part of a different Layer it will continue to hold that membership after this call as well as being a member of the new Layer. In other words, this method can be called multiple times to have an object be drawn on multiple Layers.
The AddToLayer is very simple to use. Assuming you have a valid Layer and a valid object that can be added to the Layer, you can add it as follows:
The SpriteManager's AddToLayer method can be useful but suffers a small performance penalty when called on Sprites which have already been added to the SpriteManager. For example, the following code is functional but suffers a small performance penalty.
Why is this expensive? The SpriteManager.AddSprite method tells the SpriteManager that the Sprite being added should be managed and drawn by the SpriteManager. This places the Sprite in the same category as unlayered Sprites. Calling SpriteManager.AddToLayer then places the Sprite on the argument layer. However, the SpriteManager must then remove the argument sprite from its internal SpriteLists. The following code is more efficient and preferred:
This code is functionally identical but slightly more efficient.