开发者

XNA C# Code Question

Here is my class I made to draw a HUD:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Sleyser1
{

public class Hud
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D tempHUD;
    Rectangle viewportRect;

    SpriteFont HUD;
    Vector2 FontPos;
    Vector2 FontPos2;

    public void Hud()
    {
        HUD = Content.Load<SpriteFont>("HUD");
        FontPos = new Vector2(40, 20);
        FontPos2 = new Vector2(150, 20);

        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);

 开发者_Python百科       spriteBatch.Draw(tempHUD, viewportRect, Color.White);

        string output = "Health:";
        string output2 = "Magic:";
        Vector2 FontOrigin = HUD.MeasureString(output) / 2;
        spriteBatch.DrawString(HUD, output, FontPos, Color.Red, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);
        spriteBatch.DrawString(HUD, output2, FontPos2, Color.Blue, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);

        spriteBatch.End();

    }
}
}

So how do I call it from here so that it draws.

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);


        base.Draw(gameTime);
    }

The question I am asking is how do you call a class from a method?


public void Hud()

is actually the constructor of your class, it should not be responsible for drawing (especially since you draw the same class many times and the purpose of the constructor is to ...construct a class)

So, the first step is to remove this:

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);

spriteBatch.Draw(tempHUD, viewportRect, Color.White);

string output = "Health:";
string output2 = "Magic:";
Vector2 FontOrigin = HUD.MeasureString(output) / 2;
spriteBatch.DrawString(HUD, output, FontPos, Color.Red, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(HUD, output2, FontPos2, Color.Blue, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);

spriteBatch.End();

from the constructor and add it to a new class method, such as Draw().

UPDATE:

XNA provides a Game class which seems to be the main class of the application. It should contain a reference to an object of your class.

The spritebatch is also a member of the Game class so it should be passed to the Draw function of your HUD as a parameter. Then all you need to do is call the HUD's Draw method (of a HUD object which is a reachable from the Game object) from the Game's Draw method.


Agree with the other answers here, but I'd go one further.

Turn your Hud class into a component, specifically one that that implements DrawableGameComponent. This way, you can encapsulate all your Hud logic into one place, and as the name implies, the component is able to draw itself.

So, steps :-

  • In your LoadContent method, add the following code :-

    Services.AddService(typeof(SpriteBatch), spriteBatch);

  • Create a new game component within Visual Studio 2010

  • Change the class your component inherits from. To start with, it'll be GameComponent. Change this into DrawableGameComponent
  • Override the LoadContent method. While you're not specifically loading any additional content in your example, you may wish to add Hud specific content at a later time.
  • Override the Update method. Any state changes to the Hud ( i.e. changing health or magic values ) should be made here.
  • Override the Draw method. Anything specific to drawing should go here. Note that you can get a handle to the main SpriteBatch service ( declared earlier on ) by including the following code in this overridden method :-

    SpriteBatch spriteBatch =
            Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
    
  • Finally, in the initialize method of your game class, add the following code:-

Components.Add(new HudComponent(this));

Your component will now be part of the main game loop.


Make an abstract class, for example GameElement, which contains methods Update and Draw.

Then create a (static) list of GameElement List<GameElement> Elements.

Make your class HUD inherit GameElement and implement methods Update (updates logic based on gametime) and Draw (draws the game element to the surface). When you create the HUD add it to to list Elements

In the main Draw method call foreach(var element in Elements) element.Draw().

That way you handle drawing and updating of the HUD and any other game element (Scene, Player, etc), and you never have to change the main drawing and updating loop .

You can even make GameElement an interface IGameElement so that you are not restricted to inheriting GameElement class.


You cant call a class you create an instance of it.

Classname class = new Classname();
class.method();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜