将Draw()代码移动到Class

时间:2016-04-17 02:52:02

标签: c# visual-studio monogame

我正在用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);
}

但是我希望这是一个单独的类,而不是直接放在绘制函数中,但是我不太清楚如何做到这一点并且会感谢任何帮助。

提前致谢!

1 个答案:

答案 0 :(得分: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,它按照调用的顺序绘制(即从后面到前面)。

如果您需要,可以在SpriteSortMode的通话中指定其他spriteBatch.Begin(),但对于简单的游戏,只需移动Draw来电。

SpriteSortMode MSDN如有需要,可提供更多信息。

同样,如果您愿意,可以将UpdateLoadContent方法链接到这些类中,确保将所需的任何内容作为参数传递。

<强>更新

要将gameWorld定义为GameWorld类的实例,请将其定义在游戏类的顶部附近,然后通常使用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.
}