2016-04-17 36 views
0

我創造monogame一場比賽,我已經裝磚在我的比賽中Draw()函數內部,像這樣:搬家抽獎()代碼的類

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

     spriteBatch.Begin(); 
     spriteBatch.Draw(danChar, charPosition, Color.White); 

     // loop below loads the 'grass' tiles only 

     // assuming gameworld size of 770x450 
     for (int i = 0; i < 770; i += 31) // adds 31 to i (per tile) 
     { 
      position = new Vector2(i, 392); // places incrementation into vector position 
      spriteBatch.Draw(gameTile, position, Color.White); // draws the tile each time 
      if (i == 744) 
      { 
       i = i + 26; // fills last space between 744 and 770 
       position = new Vector2(i, 392); 
      } 
      spriteBatch.Draw(gameTile, position, Color.White); 
     } 

     // loop below loads the brick tiles only (ones without grass) 

     spriteBatch.End(); // ends the spriteBatch call 
     base.Draw(gameTime); 
} 

但是我寧願認爲這是一個單獨的類,而不是直接放入繪圖函數,但我不太確定如何做到這一點,並會感謝任何幫助。

在此先感謝!

+0

多少經驗,你有寫作課? – paste

+0

您可以使用的另一件事是[DrawableGameComponent](https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gamecomponent.aspx) – LibertyLocked

回答

1

如果你只是想爲的是另一個類移動代碼,創建類(如類似GameWorld似乎適合您的代碼)

public class GameWorld 
{ 
    // You may wish to move your gameTile definition into this class if it is the only 
    // class that uses it, and handle the content loading for it in here. 
    // e.g. if you're currently loading the texture in the LoadContent method in your game 
    // class, create a LoadContent method here and pass in ContentManger as a parameter. 
    // I've passed in the texture as a parameter to the Draw method in this example to 
    // simplify as I'm not sure how you're managing your textures. 

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime, Texture2D gameTile) 
    { 
     // loop below loads the 'grass' tiles only 

     // assuming gameworld size of 770x450 
     for (int i = 0; i < 770; i += 31) // adds 31 to i (per tile) 
     { 
      Vector2 position = new Vector2(i, 392); // places incrementation into vector position 
      spriteBatch.Draw(gameTile, position, Color.White); // draws the tile each time 
      if (i == 744) 
      { 
       i = i + 26; // fills last space between 744 and 770 
       position = new Vector2(i, 392); 
      } 
      spriteBatch.Draw(gameTile, position, Color.White); 
     } 

     // loop below loads the brick tiles only (ones without grass) 
    } 
} 

然後在您的Game類的Draw方法看起來像

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

     spriteBatch.Begin(); 
     spriteBatch.Draw(danChar, charPosition, Color.White); 

     // Assuming you've created/loaded an instance of the GameWorld class 
     // called gameWorld in Initialize/LoadContent 
     gameWorld.Draw(spriteBatch, gameTime, gameTile); 

     spriteBatch.End(); // ends the spriteBatch call 
     base.Draw(gameTime); 
} 

只要確保您按照正確的順序調用Draw方法。例如您希望您的播放器出現在任何背景圖塊上方。

我相信默認SpriteSortModeDeferred,它按調用的順序(即從後面到前面)繪製。

如果您需要,您可以在撥打spriteBatch.Begin()時指定不同的SpriteSortMode,但對於簡單遊戲,只需撥打Draw來電。

更多關於SpriteSortModeMSDN如果需要。

同樣,如果您願意,您可以將您的Update,LoadContent方法鏈接到這些類中,確保傳遞任何您需要的參數作爲參數。

更新:

定義gameWorldGameWorld類的實例,你定義它靠近你的遊戲類的頂部,則通常在Initialize方法初始化。

所以你的遊戲類看起來像

public class MyGameName : Microsoft.Xna.Framework.Game 
{ 
    private SpriteBatch spriteBatch; 
    // other variable declarations 

    // Add a declaration for gameWorld 
    private GameWorld gameWorld; 

    protected override Initialize() 
    { 
     // Add the following line to initialize your gameWorld instance 
     gameWorld = new GameWorld(); 
    } 

    // other existing code - your LoadContent, Update, Draw methods etc. 
} 
+0

這幾乎可行,但我只是好奇,當你說'//假設你已經創建/加載了一個名爲gameWorld的GameWorld類的實例時,你會如何做到這一點,因爲我似乎有一個小小的錯誤。 – toadflax

+1

我已經更新了答案,以展示如何聲明/初始化gameWorld。請注意,代碼只是一個簡短的示例,其中會包含很多其他代碼。這只是爲了說明語法和線路的位置。 – Tone