Noob! Creating a simple sprite class.

Hi folks.

Not only am I a rusty programmer, I’m using VB and I’m completely new to Mono. Advise me against VB as much as you like, I can assure you C# ain’t going to happen to this old timer.

Anyway, I’m trying to create a class for a very simple moving sprite, but I’m a bit confused about how to do it. I’m fine with single items of texture2d, or arrays of texture2d, but how do you create a class object of a texture2d?

Any help gratefully received.

(I can transpose C#!)

1 Like

You can load a Texture2D using the Content Manager, e.g. (in Game1, also C# because I’m used to it)

this.Content.Load<Texture2D>("name_without_extension")

The textures themself you have to add to the Monogame Content Project in the Content folder of your project, make sure you build that as well.

For a moveable sprite i would recommend storing the texture itself in a central space or declaring it static so you can avoid loading it multiple times. In the class you would have a position (Vector2) and update/draw methods. In draw you would call .Draw() on the spritebatch with the previously loaded Texture2D and the position, in Update you would move the sprite by changing the position.

Hope this helps!

Yeah, as I say, I’m fine getting everything working within the Game class.

I’m trying to create a new Class of sprite object, but if I create a class that inherits texture2d I can’t assign ‘Load’ the texture in the main routine or in the class as there’s no load routine.

It must be pretty easy to create a class for a sprite? Or with Mono, does everything have to happen within the one main class Game1?

Don’t inherit texture2d? Just create a texture2d variable. Anways contact me on skype: Acrew1979. I’ll send you a book that was a GREAT learning example for me. Only problems I had from that point was collision that wasnt weird but I just use farseer physics now.

A simple sprite class could look something like this (In c#)

public class Sprite
{
    public Vector2 Position;
    private Texture2D texture;

    public Sprite(Texture2D texture)
    {
        this.texture = texture;
    }

    public void Draw(SpriteBatch sp)
    {
        sp.Draw(texture, position, null, Color.white);
    }
}

Then you could load the spirtes like this (in Game1.cs):

public class Game1 : Game
{
    private Sprite sprite1;

    protected override void LoadContent()
    {
        sprite1 = new Sprite(Content.Load<Texture2D>("YourSpriteName"));
        sprite1.Position.X = 100;
        sprite1.Position.Y = 20;
    }

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

        spriteBatch.Begin();
        sprite1.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

Hope any of the above helps you out even a bit.

2 Likes

Hey so this might seem obvious but what exactly does this do?
> this.texture = texture;

I’m seeing more and more of the “this.texture” piece of syntax especially but I cant figure out what it does exactly

private Texture2D texture;//this <<<

public Sprite(Texture2D texture)//parameter name same as declared field
{
    //this. use to tell texture from class not parameter
    this.texture = texture;
}

more… link

1 Like