2017-04-15 21 views
0

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

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(); 
     } 
    } 
+0

你得到一个错误? – CodingYoshi

+0

没有错误,当我编译时它不显示我的对象。在另一个叫做player的类中,我应该引用Game1类中的其他类吗? –

+0

@Liam Earle,你在哪里创建一个播放器? – vyrp

回答

0
  • 负载,更新和Draw方法属于继承的游戏等级和被它overrided。
  • 你也需要启动你的SpriteBacth对象。
  • GraphicsDevice对象存在于Game主类中。

试试这个:

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