1
votes

I'm trying to program a game with a friend, and I'm very confused as to how to draw sprites. I can draw code in my Game1 class, below.

namespace SweetGeorgiaBrown
{
    /// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
            enum GameStates { TitleScreen, Overworld, Menu, Battle, GameOver };
    GameStates gameState = GameStates.Battle;
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D dinoTex;
    Texture2D alienTex;
    Texture2D robotTex;
    Texture2D eroboTex;
    SpriteFont basic;


    private Vector2 textLocation = new Vector2(20, 20);
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";


    }

    public class Health
    {
        public static int dinoHP = 300;
        public static int alienHP = 200;
        public static int robotHP = 100;
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        dinoTex = Content.Load<Texture2D>(@"Sprites\dino\dinoFrontOvwld");
        alienTex = Content.Load<Texture2D>(@"Sprites\alien\alienFrontOvwld");
        robotTex = Content.Load<Texture2D>(@"Sprites\robot\robotFrontOvwld");
        eroboTex = Content.Load<Texture2D>(@"Sprites\robo1\robo1OvwldFront");

        basic = Content.Load<SpriteFont>(@"Fonts\Basic");
        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        // TODO: Add your update logic here
        switch (gameState)
        {
            case GameStates.Battle:
                break;
        }
        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();

        // TODO: Add your drawing code here
        if ((gameState == GameStates.Battle))
        {
            spriteBatch.DrawString(
                basic,
                "ASDFASDFADFASDFA ", textLocation, Color.Black);
            spriteBatch.Draw(
                dinoTex, textLocation, Color.White);
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

}

In my Game1 code, I can successfully draw that sprite and the text there on the screen. However, I want to draw a sprite in another class, but I have absolutely no idea what to put there.

In the other class, I wish I could just do something as simple as

sprite.Draw(//here would be x and y coords of where the sprite goes, name of the texture), and have that be it, but it seems to just not work that way. Does anyone know how I can go about doing this?

1

1 Answers

3
votes

It does indeed work like that. Extract your sprite specific code into a class that is structured something similar to:

public Sprite()
{
}

public void Update(GameTime gameTime)
{
}

public void Draw(SpriteBatch spriteBatch)
{
}

Then in your game1 class you can call

Sprite.Update(gameTime); 

and

Sprite.Draw(spriteBatch);

That should help you get started. Did you try some approaches yourself, if so post them and we can see where they went wrong, or are you just hoping for a quick answer you can copy and paste?