如何在XNA中的列表中绘制两个Texture2D?

时间:2015-12-04 16:26:09

标签: c# xna xna-4.0

我有一个名为blockTexture的Texture2D列表,其中包含2个元素。

我使用以下行绘制第一个元素

foreach (Vector2 blockPosition in blockPositions)
 spriteBatch.Draw(blockTexture[0], blockPosition, Color.White);

blockPosition列表用于显示随机位置的元素

我可以让它同时绘制两个元素吗?

如果我将blockTexture [0]更改为blockTexture [1],则会绘制第二个元素。但我需要它同时画两个。

我该怎么做?

编辑:

Texture2D personTexture;
// The images will be drawn with this SpriteBatch
SpriteBatch spriteBatch;
// Person 
Vector2 personPosition;
const int PersonMoveSpeed = 5;

List<Vector2> blockPositions = new List<Vector2>();
float BlockSpawnProbability = 0.01f;
const int BlockFallSpeed = 2;

List<Texture2D> blockTexture = new List<Texture2D>();

Random random = new Random();

protected override void LoadContent()         {

        blockTexture.Add(Content.Load<Texture2D>("Block"));
        blockTexture.Add(Content.Load<Texture2D>("right"));

        // Load textures
      // blockTexture = Content.Load<Texture2D>("Block");
       personTexture = Content.Load<Texture2D>("Person");
       //rightTexture = Content.Load<Texture2D>("Right");

        //Load the score font
        font = Content.Load<SpriteFont>("gameFont");

        // Create a sprite batch to draw those textures
        spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
    }

    protected override void Update(GameTime gameTime)
    {
        // Get input
        KeyboardState keyboard = Keyboard.GetState();
        GamePadState gamePad = GamePad.GetState(PlayerIndex.One);

        // Spawn new falling blocks 
        if (random.NextDouble() < BlockSpawnProbability)
        {
            float x = (float)random.NextDouble() * (Window.ClientBounds.Width - blockTexture[0].Width);
            blockPositions.Add(new Vector2(x, -blockTexture[0].Height));
       }

        // Get the bounding rectangle of the person
        Rectangle personRectangle =
            new Rectangle((int)personPosition.X, (int)personPosition.Y,
            personTexture.Width, personTexture.Height);

        // Update each block
        personHit = false;
        for (int i = 0; i < blockPositions.Count; i++)
        {
            // Animate this block falling
            blockPositions[i] =
                new Vector2(blockPositions[i].X, blockPositions[i].Y + BlockFallSpeed);

             // Get the bounding rectangle of this block
                Rectangle sprite =
                    new Rectangle((int)blockPositions[i].X, (int)blockPositions[i].Y,
                    blockTexture[0].Width, blockTexture[0].Height);


                // check collision with person
                if (personRectangle.Intersects(sprite))
                    personHit = true;
                // Remove this block if it have fallen off the screen
                if ((blockPositions[i].Y > Window.ClientBounds.Height))
                {
                    blockPositions.RemoveAt(i);
                    // When removing a block, the next block will have the same index
                    // as the current block. Decrement i to prevent skipping a block.
                    i--;
                }
            }

            base.Update(gameTime);
        }

并在绘图部分:

foreach (Vector2 blockPosition in blockPositions)
            {
                //foreach (Texture2D blocktexture in blockTexture)
               //spriteBatch.Draw(blockTexture[1], blockPosition, Color.White);
                for (int i = 0; i < blockPositions.Count; i++ )
                {
                    spriteBatch.Draw(blockTexture[i], blockPositions[i], Color.White);


                }

1 个答案:

答案 0 :(得分:0)

尝试在foreach循环中按顺序显式绘制:

foreach (Vector2 blockPosition in blockPositions)
{
    spriteBatch.Draw(blockTexture[0], blockPosition, Color.White);
    spriteBatch.Draw(blockTexture[1], blockPosition, Color.White);
}

或者您是想在blockTexture[0]blockPositions[0] blockTexture[1]处绘制blockPositions[1]?在那种情况下:

for (int i = 0; i < blockPositions.Length; i++)
{
    spriteBatch.Draw(blockTexture[i], blockPositions[i], Color.White);
}