正在将Draw()代码移动到类中

本文关键字:代码移动 Draw | 更新日期: 2024-09-24 21:18:59

我正在一夫一妻制中创建一个游戏,我已经在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);
}

然而,我更希望这是一个单独的类,而不是直接放在draw函数中,但我不太确定如何做到这一点,如果能提供任何帮助,我将不胜感激。

提前感谢!

正在将Draw()代码移动到类中

如果您只想将代码按原样移动到另一个类,请创建您的类(例如,类似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调用即可。

有关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.
}