开发者

Using DrawableComponent correctly

开发者 https://www.devze.com 2022-12-14 00:05 出处:网络
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 t

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 GameComponents, 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 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号