I wanted to extend this class for a GUI layer 开发者_运维问答on top of my game. I can't find much information on actually using the IComponent, IDrawable series of interfaces. It seems that most of the examples I've looked at, the developer is handling all of this on their own.
What is the appropriate way to use these interfaces?
As noted before, you should derive your class from DrawableGameComponent
. Then you should implement
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
// TODO: Add your update code here
base.Draw(gameTime);
}
It's important to create your DrawableGameComponent
in its container's (typically your Game1
class) constructor and add to Game.Components
in the same place, otherwise your components methods might not get called correctly.
If you manage your components this way, Initialize
, Update
and Draw
will be called automatically by the framework.
The same goes for GameComponent
s, but obviously without the Draw
method.
Once you add the component to the list of components, the xna game framework takes care of calling all of the appropriate methods for you. In practice, no one ever really implements the interfaces directly, they just use one of the built in components like DrawableGameComponent (As @Peter pointed out).
Here is an example/tutorial on how to use it :-)
精彩评论