在下面的代码中,我认为它已经正确完成了。但是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文件。我把它放在项目的Content文件夹中。我已将'内容'和'如果更新则复制'设置为文件属性。
这是我在转换为xnb之前使用的.png文件:https://imageshack.us/scaled/large/15/loadingo.png
或许问题出在我的.png文件中?
有什么想法吗?感谢
答案 0 :(得分:1)
第二次sprite批量调用很奇怪。在我看来,你正在移动和/或在屏幕外旋转图像。
由于您正在使用this overload绘图,因此您实际上是这样做的:
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时我曾经看过这个,并且不得不改变它以使用视口宽度和高度。
答案 1 :(得分: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绘制到可见屏幕。
答案 2 :(得分: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调用中使用图层。
正如我所说,我不确定你为什么要这样,所以我可能会错,但这样做这个简单的方法将锻炼它的纹理文件或其他东西。
修改强>
根据您的评论,id说要尝试通过坐标定位texture2D e.g。
_spriteBatch.Draw(_bg, new vector2(0,0), color.black);
或
_spriteBatch.Draw(_bg, new vector2(screen.width/2,screen.height/2), color.black);
如果你仍然看不到它的建议可能会查看该文件的属性或将文件更改为jpg进行测试。
答案 3 :(得分:0)
这是解决方案(同样的问题,我发布在不同的网站上)
https://gamedev.stackexchange.com/questions/54672/why-spritebatch-draw-shows-blank-output