将png绘制到SpriteBatch不起作用

时间:2016-02-07 19:56:27

标签: c# xna sprite monogame spritebatch

我刚刚开始处理MonoGame,我只是想在Sprite课程中加载简单的Game,如下所示:

public class Main : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D test;

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

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            test =  Content.Load<Texture2D>("default");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(test, Vector2.Zero, Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }

但它不起作用,这里缺少什么?

2 个答案:

答案 0 :(得分:0)

我认为这是因为您将类传递给spriteBatch = new SpriteBatch(GraphicsDevice);构造函数而不是它的实例。

尝试将spriteBatch = new SpriteBatch(graphics.GraphicsDevice);替换为spriteBatch.Begin ((SpriteSortMode)0, BlendState.NonPremultiplied)

我在使用"watch":true为我工作时,在使用Alpha通道在MonoGame中工作时也遇到了一些麻烦。

答案 1 :(得分:0)

通常,您需要指定要加载的图像文件的类型,尝试添加&#34; .png&#34;到#34;默认&#34;的结尾。因为您说图像正确加载,这可能无法解决您的问题,但我建议尝试一下。

如果这不起作用,问题可能在于您的操作系统以及Monogame和Visual Studio的版本;我没有绘制图像的问题,但在Visual Studio中使用冲突的版本和设置编译其他所有内容。希望有所帮助!