0

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);

        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?

1
  • 2 Lines of actual question and loads of code! Why not explain a bit more.
    – Shoban
    Jun 13, 2011 at 7:01

4 Answers 4

3
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.

2
  • This works great exept for this line: HUD = Content.Load<SpriteFont>("HUD"); the Content is red undersquiggled and say it does not exist in current context. Jun 13, 2011 at 8:35
  • @Crash_Override: do something similar to what you did for the Draw method, this time for the LoadContent. You should not do that either in the constructor... And find a tutorial :)
    – Andrei
    Jun 13, 2011 at 8:40
1

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.

2
  • I don't know what you mean by this line: Create a new game component within Visual Studio 2010 Jun 13, 2011 at 8:25
  • Add New Item -> Game Component Jun 13, 2011 at 9:44
0

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.

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.