Monkeybeat
Monkeybeat

Reputation: 13

Remove objects from lists monogame

I need help to remove the blocks that I created. When all the 2000 thousand blocks are removed I need the program to close itself. I have tried several things but my RemoveAt function never seems to work as intended. What am I doing wrong?

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace projectblob_nivå1
{
class Block
{
    public int X { get; set; } = 100;
    public int Y { get; set; } = 100;
    public Color Color { get; set; } = Color.Red;

    public void Remove()
    {
        int i = 0;
        block.RemoveAt(i);
        i++;
    }


    public void Draw(SpriteBatch spriteBatch, Texture2D texture)
    {
        spriteBatch.Draw(texture, new Rectangle(X, Y, 30, 30), Color);
    }


}

}

and then Game.cs

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;
    Texture2D pixelTexture;
    List<Block> blocks = new List<Block>();

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        Random rnd = new Random();
        // TODO: Add your initialization logic here
        for (int i = 0; i < 2000; i++)
        {
            var block = new Block();
            block.X = rnd.Next(0, 600);
            block.Y = rnd.Next(0, 400);
            block.Color = new Color(rnd.Next(256), rnd.Next(256), rnd.Next(256));
            blocks.Add(block);
        }
        base.Initialize();
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        pixelTexture = Content.Load<Texture2D>("pixel");
        // TODO: use this.Content to load your game content here
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        foreach (Block block in blocks)
        {
            int i = 0;
            block.RemoveAt(i);
            i++;
            if (i==2000)
            {
                Exit();
            }
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        _spriteBatch.Begin();
        foreach (Block block in blocks)
        {
            block.Draw(_spriteBatch, pixelTexture);
        }
        _spriteBatch.End();

        base.Draw(gameTime);
    }
}

would be thankful for all the help I can get :)

Upvotes: 0

Views: 475

Answers (1)

Victor
Victor

Reputation: 109

i is set inside the foreach so will always have value 0 at every loop.

    foreach (Block block in blocks)
    {
        int i = 0;
        block.RemoveAt(i);
        i++;
        if (i==2000)
        {
            Exit();
        }
    }

you need to do like this

    int i = 0;
    foreach (Block block in blocks)
    {
        block.RemoveAt(i);
        i++;
        if (i==2000)
        {
            Exit();
        }
    }

Also it should be blocks.RemoveAt(i) not block.RemoveAt(i).

If you only want to empty the list, why not do

    blocks = new List<Blocks>();

    

Upvotes: 1

Related Questions