【问题标题】:Sprite won't draw in SplashScreen ClassSprite 不会在 SplashScreen 类中绘制
【发布时间】:2017-05-13 00:32:11
【问题描述】:

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

这是我的代码:

游戏1:

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);
    }
}

游戏画面:

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);
    }
}

【问题讨论】:

    标签: c# xna monogame xna-4.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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多