Sprite不会在SplashScreen Class中绘制

时间:2017-05-13 00:32:11

标签: c# xna monogame xna-4.0

所以我一直在关注CodingMadeEasy的RPG教程,只是为了掌握C#和XNA / Monogame。我做了他所做的一切,但我的精灵仍然没有在屏幕上画画。

这是我的代码:

的Game1:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {
        graphics.PreferredBackBufferWidth = (int)ScreenManager.Instance.Dimensions.X;
        graphics.PreferredBackBufferHeight = (int)ScreenManager.Instance.Dimensions.Y;

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        ScreenManager.Instance.LoadContent(Content);
    }

    protected override void UnloadContent()
    {
        ScreenManager.Instance.UnloadContent();

        base.UnloadContent();
    }

    protected override void Update(GameTime gameTime)
    {
        ScreenManager.Instance.Update(gameTime);

        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        {
            Exit();
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin();
        ScreenManager.Instance.Draw(spriteBatch);
        spriteBatch.End();

        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);
    }
}

屏幕管理:

public class ScreenManager
{
    private static ScreenManager instance;
    public Vector2 Dimensions { private set; get; }
    public ContentManager Content { private set; get; }

    GameScreen currentScreen;

    public static ScreenManager Instance
    {
        get 
        {
            if (instance == null)
                instance = new ScreenManager();

            return instance;
        }
    }

    public ScreenManager()
    {
        Dimensions = new Vector2(640, 480);
        currentScreen = new SplashScreen();

    }

    public void LoadContent(ContentManager Content) 
    {
        this.Content = new ContentManager(Content.ServiceProvider, "Content");
        currentScreen.LoadContent();
    }

    public void UnloadContent()
    {
        currentScreen.UnloadContent();
    }

    public void Update(GameTime gameTime)
    {
        currentScreen.Update(gameTime);
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        currentScreen.Draw(spriteBatch);
    }
}

GameScreen:

public class GameScreen
{
    protected ContentManager content;

    public virtual void LoadContent()
    {
        content = new ContentManager(
            ScreenManager.Instance.Content.ServiceProvider, "Content");
    }

    public virtual void UnloadContent()
    {
        content.Unload();
    }

    public virtual void Update(GameTime gameTime)
    {

    }

    public virtual void Draw(SpriteBatch spriteBatch)
    {

    }
}

闪屏:

public class SplashScreen : GameScreen
{
    Texture2D image;
    string path;

    public override void LoadContent()
    {
        base.LoadContent();
        path = "SplashScreen/Image";
        image = content.Load<Texture2D>(path);
    }

    public override void UnloadContent()
    {
        base.UnloadContent();
    }

    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

    public override void Draw(SpriteBatch spriteBatch)
    {

        spriteBatch.Draw(image, Vector2.Zero, Color.White);

        base.Draw(spriteBatch);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在绘制精灵之前清除屏幕。在上面提供的代码中,您正在绘制精灵,然后立即清除屏幕。

这应该解决它:

protected override void Draw(GameTime gameTime)
{
    // move this here
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    ScreenManager.Instance.Draw(spriteBatch);
    spriteBatch.End();

    base.Draw(gameTime);
}