Question

I'm about ready to tear my hair out because spriteBatch() is just not "clicking" with me. I can't seem to find anything online that covers my use of this in this exact situation, which makes me believe I'm missing something incredibly simple.

I've got a class in my XNA project called player that, surprisingly, controls player, physics etc. But I'm taking it one step at a time. First step: Draw the player in the level. Level code works great, no worries. But I cannot get the player to draw.

I've got a basic Texutre2D texture for a sprite I drew up in paint. Going by this tutorial, I should just be able to do the following:

public void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        playerSprite = Level.Content.Load<Texture2D>("player/pory");

    }

public void Draw()
    {
        graphics.GraphicsDevice.Clear(Color.Beige);

        // Draw the sprite.
        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        spriteBatch.Draw(playerSprite, Position, Color.White);
        spriteBatch.End();

    }

And it should just work. But no. I am missing a few things in this player class of mine. 1) in LoadContent(), I can't create a new SpriteBatch() because GraphicsDevice does not exist and can't just be created. It appears in the Game.cs and is initialized with this meaning the game itself. Okay. But I can't reference Game.GraphicsDevice, either.

That's basically the whole issue, everything else stems from this. I can't find a way around it. Does anyone know the proper way to try and do this? I can't seem to find exactly what I'm missing to get this to work.

Était-ce utile?

La solution

The easiest way to solve this is pass your Player class the instance of GraphicsDevice and SpriteBatch in the constructor when you create it inside your Game class.

player = new Player(graphicsDevice, spriteBatch );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top