1

在我下面的代码中,我认为它是正确完成的。但是idk,_bg调用的图像没有显示出来。屏幕只显示白色。奇怪的是,当我运行它时出现 0 错误消息。

(RenderTarget2D 用于将我的视图默认设置为横向模式)

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameName2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        Texture2D _bg;
        RenderTarget2D finalImageTarget;

        public Game1()
        {
            _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);

            _bg = Content.Load<Texture2D>(@"background");
            finalImageTarget = new RenderTarget2D(GraphicsDevice, 1280, 720);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all 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)
        {
            // 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.SetRenderTarget(finalImageTarget);
            GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here
            _spriteBatch.Begin();
            _spriteBatch.Draw(_bg,
                new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
                null,
                Color.White,
                0,
                Vector2.Zero,
                SpriteEffects.None,
                0);
            _spriteBatch.End();

            GraphicsDevice.SetRenderTarget(null);
            _spriteBatch.Begin();
            _spriteBatch.Draw(finalImageTarget,
                    new Vector2(720,0),
                    null,
                    Color.White,
                    MathHelper.PiOver2,
                    Vector2.Zero,
                    Vector2.One,
                    SpriteEffects.None,
                    0f);
            _spriteBatch.End();

            base.Draw(gameTime);


        }
    }
}

我使用 xnb 文件。我把它放在项目的内容文件夹中。我已将“内容”和“如果较新则复制”设置为文件属性。

这是我在转换为 xnb 之前使用的 .png 文件:https ://imageshack.us/scaled/large/15/loadingo.png

或者问题出在我的 .png 文件中?

任何想法?谢谢

4

4 回答 4

1

第二个精灵批处理调用相当奇怪。在我看来,您正在移动和/或旋转屏幕外的图像。

由于您使用的是 draw 的重载,因此您实际上是在执行以下操作:

Texture2D texture =          finalImageTarget;
Vector2 position =           new Vector2(720,0);
Rectangle? sourceRectangle = null;
Color color =                Color.White;
float rotation =             MathHelper.PiOver2;
Vector2 origin =             Vector2.Zero;
Vector2 scale =              Vector2.One;
SpriteEffects effects =      SpriteEffects.None;
float layerDepth =           0f;

_spriteBatch.Draw (texture,position,sourceRectangle,color,rotation,origin,scale,effects,layerDepth,layerDepth);

这意味着您将图像定位在屏幕左上角右侧 720 像素处,然后将其围绕图像的左上角旋转 90 度。

我的建议是从简单的开始,逐步达到你想要达到的目标。使用没有渲染目标的单个精灵批处理调用,并使用没有旋转的源和目标矩形。

编辑:使用调试器确保所有变量都带有您希望看到的值。例如,Window.ClientBounds.Width / Height 可能会显示为零。我想我在移植到 Android 时看到过一次,不得不将其更改为使用视口宽度和高度。

于 2013-04-28T23:34:24.683 回答
0

这是解决方案(同样的问题,我在不同的网站上发布)

https://gamedev.stackexchange.com/questions/54672/why-spritebatch-draw-shows-blank-output

于 2013-04-29T16:17:26.643 回答
0

你为什么使用 Render2D?有什么具体原因吗?

如果不是,那么我将通过使用更改窗口的分辨率

_graphics.PreferredBackBuffer.Width = 720;
_graphics.PreferredBackbuffer.Height = 1280;

在你的构造函数中。然后您可以使用以下内容自由放置 Texture2D 图像;

_spriteBatch.Begin();
        _spriteBatch.Draw(_bg,
            new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
            null,
            Color.Black,
            0,
            Vector2.Zero,
            SpriteEffects.None,
            0);
        _spriteBatch.End();

如果您担心放置在背景图像上方和下方,请尝试在 spriteBatch 调用中使用图层。

正如我所说,我不确定你为什么要这样,所以我可能是错的,但这样做很简单,不管它是你的纹理文件还是其他东西。

编辑

根据您的评论,我说尝试通过坐标定位 texture2D,例如

_spriteBatch.Draw(_bg, new vector2(0,0), color.black);

或者

_spriteBatch.Draw(_bg, new vector2(screen.width/2,screen.height/2), color.black);

如果您仍然看不到它,建议您查看文件的属性或将文件更改为 jpg 进行测试。

于 2013-04-29T12:55:32.907 回答
0
        GraphicsDevice.SetRenderTarget(finalImageTarget);
        GraphicsDevice.Clear(Color.White);

        // TODO: Add your drawing code here
        _spriteBatch.Begin();
        _spriteBatch.Draw(_bg,
            new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
            null,
            Color.White,
            0,
            Vector2.Zero,
            SpriteEffects.None,
            0);
        _spriteBatch.End();

此代码未绘制到您看到的屏幕上。它正在绘制到finalImageTarget渲染目标。渲染目标是一个绘制的地方,它不会显示在屏幕上。将其设置为GraphicsDevice.SetRenderTarget(null);告诉 XNA 绘制到可见屏幕。

于 2013-04-28T21:49:19.663 回答