在animationSprite.cs中,我有:
public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width / Columns;
int height = Texture.Height / Rows;
int row = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;
Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, 120, 140);
if (Game1.przelacznik_w_bezruchu == true) { sourceRectangle = new Rectangle(0, 0, width, height); }
spriteBatch.Begin();
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
spriteBatch.End();}
在GameScene.cs
private void DrawGame(){
spriteBatch.Begin();
spriteBatch.Draw(floor1, rec_floor1, color1);
spriteBatch.Draw(floor2, rec_floor2, color1);
spriteBatch.Draw(house1, rec_house1,color1);
spriteBatch.End();}
我希望地板和房屋的高度低于子画面,以免遮挡子画面。但是我不知道如何为纹理分配深度级别。
答案 0 :(得分:1)
SpriteBatch Draw函数有一个重载,允许您指定layerDepth。
您可以在Draw调用中对animationSprite.cs使用以下内容:
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, layerDepth);
layerDepth将是一个float值,它允许您控制子画面的排序顺序。这可以从您的GameScene.cs作为float参数传入,也可以将location参数更改为Vector3并使用其Z变量。
其他参数:
答案 1 :(得分:0)
问题是您有多个批次,定义为spriteBatch.Begin()
和spriteBatch.End()
之间的代码。
默认情况下,spriteBatch.Begin()
是不带任何参数的调用,所有精灵均按其调用顺序绘制:第一个绘制调用是背景。第二个抽奖电话将在第一个抽奖电话的顶部...
@ProfessionalKent在回答中指出,请使用layerDepth
参数:
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, layerDepth);
layerDepth接受任何float
值(默认值为0),较低的值将呈现在较高的值之上。任何相同的值都按照输入的顺序进行。
此参数也适用于spriteBatch.DrawString()
但是,layerDepth
仅在同一批次中有效。一旦调用End()
,该批次将被排序,展平并呈现到帧缓冲区。
写下所有绘制对象的所需深度。在下面的代码中替换layerDepth
数字以反映您的分析。
删除整个项目中的所有spriteBatch.Begin()
和spriteBatch.End()
行。除非spriteBatch.Begin()
具有非default(第81和91-100行)参数。
在Game1.cs
之后,尽早在GraphicsDevice.Clear
中打开spriteBatch:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// Your existing code here.
spriteBatch.End();
//oprozniony, nothing should be here
}
在GameScene.cs中:
private void DrawGame()
{
// The ,, is for the null source rectangle,
spriteBatch.Draw(floor1, rec_floor1,, color1, 0.0f, Vector2.Zero, SpriteEffects.None, 2);
spriteBatch.Draw(floor2, rec_floor2,, color1, 0.0f, Vector2.Zero, SpriteEffects.None, 3);
spriteBatch.Draw(house1, rec_house1,,color1, 0.0f, Vector2.Zero, SpriteEffects.None, 1);
// example of a table the animatedSprite(at a depth of 0) will walk or move behind(note the negative depth):
// spriteBatch.Draw(stol, rec_stol, , color1, 0.0f , Vector2.Zero, SpriteEffects.None, -1);
}
在AnimatedSprite.cs中
public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
// unless the width and height can change when the game is running, they should be class variables set in the constructor
int width = Texture.Width / Columns;
int height = Texture.Height / Rows;
// removed unnecessary casts:(unless currentFrame is not an int)
int row = currentFrame / Columns;
int column = currentFrame % Columns;
Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, 120, 140);
if (Game1.przelacznik_w_bezruchu == true) sourceRectangle = new Rectangle(0, 0, width, height);
// Place the sprite at Depth of 0.
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
}
在某些情况下需要多个批次。如果是这种情况,请按深度顺序手动拆分批次。