2017-04-15 70 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(); 
    } 
}