Xna 为什么spritebatch.Draw显示空白输出?

Xna 为什么spritebatch.Draw显示空白输出?,xna,windows-phone-8,texture2d,monogame,Xna,Windows Phone 8,Texture2d,Monogame,在我下面的代码中,我认为这是正确的。但是idk,由_bg调用的图像没有显示。屏幕上只显示白色。奇怪的是,当我运行它时,没有错误消息 (RenderTarget2D用于将我的视图默认设置为横向模式) 使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Graphics; 命名空间GameName2 { /// ///这是游戏的主要类型 /// 公共类游戏1:游戏 { GraphicsDeviceManager\u graphics; Spr

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

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

使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
命名空间GameName2
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:游戏
{
GraphicsDeviceManager\u graphics;
SpriteBatch_SpriteBatch;
纹理2d_bg;
RenderTarget2D finalImageTarget;
公共游戏1()
{
_graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
_spriteBatch=新spriteBatch(图形设备);
_bg=内容加载(@“背景”);
finalImageTarget=新的RenderTarget2D(GraphicsDevice,1280720);
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//TODO:在此处添加更新逻辑
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
GraphicsDevice.SetRenderTarget(finalImageTarget);
图形设备。清晰(颜色。白色);
//TODO:在此处添加图形代码
_spriteBatch.Begin();
_spriteBatch.Draw(_bg,
新矩形(0,0,Window.ClientBounds.Width,Window.ClientBounds.Height),
无效的
颜色,白色,
0,
向量2,零,
精神效应,没有,
0);
_spriteBatch.End();
GraphicsDevice.SetRenderTarget(空);
_spriteBatch.Begin();
_spriteBatch.Draw(最终图像目标,
新矢量2(720,0),
无效的
颜色,白色,
MathHelper.PiOver2,
向量2,零,
矢量2.1,
精神效应,没有,
0f);
_spriteBatch.End();
基础。抽签(游戏时间);
}
}
}
我使用xnb文件。我把它放在项目的内容文件夹中。我已将“内容”和“如果较新,则复制”设置为文件属性

这是我在转换为xnb之前使用的.png文件:

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

有什么想法吗?谢谢

        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绘制到可见屏幕


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

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

由于您使用的是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度

我的建议是从简单做起,努力达到你想要的目标。使用不带渲染目标的单个sprite批处理调用,并使用不带旋转的源矩形和目标矩形


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

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

由于您使用的是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度

我的建议是从简单做起,努力达到你想要的目标。使用不带渲染目标的单个sprite批处理调用,并使用源和目标
_graphics.PreferredBackBuffer.Width = 720;
_graphics.PreferredBackbuffer.Height = 1280;
_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.Draw(_bg, new vector2(0,0), color.black);
_spriteBatch.Draw(_bg, new vector2(screen.width/2,screen.height/2), color.black);