我在这个Monogame课上做错了什么?

时间:2017-04-15 21:46:27

标签: c# monogame

我在这堂课上做错了什么?我正在使用monogame和C#,但我的对象不会在程序中呈现。

class Player : Game
    {
        Texture2D PlayerSprite;
        Vector2 PlayerPosition;


        public Player()
        {
            Content.RootDirectory = "Content";
            PlayerSprite = Content.Load<Texture2D>("spr_Player");
            PlayerPosition = Vector2.Zero;
        }

        public void Update()
        {


        }

        public void Draw(SpriteBatch SpriteBatch)
        {
            SpriteBatch.Begin();
            SpriteBatch.Draw(PlayerSprite,PlayerPosition, new Rectangle(0, 0, 32,32), Color.White);
            SpriteBatch.End();
        }
    }

1 个答案:

答案 0 :(得分:1)

  • Load,Update和Draw方法属于继承的Game类,并被它覆盖。
  • 您还需要启动SpriteBacth对象。
  • 在Game主类中存在GraphicsDevice对象。

试试这个:

class Player : Game
{
    Texture2D PlayerSprite;
    Vector2 PlayerPosition;
    SpriteBatch spriteBatch;

    public Player()
    {
        Content.RootDirectory = "Content";
        PlayerSprite = Content.Load<Texture2D>("spr_Player");
        PlayerPosition = Vector2.Zero;
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
    }

    protected override void Update(GameTime gameTime)
    {
    }

    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(PlayerSprite,PlayerPosition, new Rectangle(0, 0, 32,32), Color.White);
        spriteBatch.End();
    }
}