为什么我不能在我的数组中创建更多PictureBox?

时间:2016-04-04 13:05:31

标签: c#

首先,我想感谢你的时间:)所以我有问题..我正在尝试制作一个小游戏,我生成 PictureBox 并从右到左发送它我的播放器将是 PictureBox 将尽量避免它们跳跃。所以我做的第一件事是,我用类创建了我的PictureBox spawner ..但问题是,我只能生成块[0] ..当我尝试创建块[1]时,没有任何反应!请帮帮我..有我的代码:

Form1.cs的

namespace Game_0._1
{
    public partial class Form1 : Form
    {
        Block[] block = new Block[50];

        public Form1()
        {
            InitializeComponent();
            int _Length = block.Length;

            for(int i=0; i < 50; i++)
            {
                block[i] = new Block();
                this.Controls.Add(block[i]._ScreenPanel());
                block[i].Screen.Controls.Add(block[i].Box);
                label1.Text = ("Generating block [" + i + "]");

            }    

        }

        private void button1_Click(object sender, EventArgs e)
        {
            block[0].SpawnBlock();
        }
    }
}

Block.cs

namespace Game_0._1
{
    class Block
    {
        public Panel Screen;
        public PictureBox Box;
        Point x = new Point(50, 50);
        Size s = new Size(150, 50);
        Color c = Color.FromName("black");

        public Block()
        {
            Screen = new Panel();
            Screen.Dock = DockStyle.Fill;
            Box = new PictureBox();

        }
        public Panel _ScreenPanel()
        {
            return Screen;
        }
        public PictureBox SpawnBlock()
        {
            Box.Name = "Obstacle";
            Box.Location = x;
            Box.Size = s;
            Box.BorderStyle = BorderStyle.FixedSingle;
            Box.BackColor = c;
            return Box;
        }
        public void ChangeXLoc()
        {
            this.x.X += 50;
        }


    }
}

这里:

private void button1_Click(object sender, EventArgs e)
            {
                block[0].SpawnBlock();
            }

这成功地产生了一个黑盒子,但是如果我输入block [1],则没有......

2 个答案:

答案 0 :(得分:1)

问题是,您的Panel(屏幕)与框[1]所在的Panel重叠,因此除非被带到前面,否则它不可见 - 这反过来会使另一个盒子不可见。

对此的解决方案是使用单个面板,并相应地调整大小,这允许您将每个框放置在同一个面板上并使其相对于面板0,0

另一种解决方案是为每个盒子设置一个与盒子大小相同的面板,并且移动面板而不是盒子,这将具有相同的效果,但可能需要更少的代码被编辑。

答案 1 :(得分:0)

我修改了你的代码可能有点帮助:

你的表格:

public partial class Form1 : Form
{
    private Block[] _block = new Block[50];
    private Point _x = new Point(0, 50);
    private  Timer _timer=new Timer();
    private int _index = 0;

    public Form1()
    {
        InitializeComponent();
        _timer.Interval = 1000;
        _timer.Tick += _timer_Tick;
        for (int i = 0; i < 50; i++)
        {
            _block[i] = new Block(_x);
             //Move the position of the block
            ChangeXLoc();
            Controls.Add(_block[i]._ScreenPanel());
            label1.Text = @"Generating block [" + i + @"]";
        }
    }
    //Timer to spawn the blocks
    private void _timer_Tick(object sender, EventArgs e)
    {
        _block[_index].SpawnBlock();
        if (_index < 49)
            _index++;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _timer.Start();
    }

    private void ChangeXLoc()
    {
        _x.X += 50;
    }
}

和你的街区类:

class Block
    {
        Panel Screen;
        PictureBox Box;
        Point _x;
        Size s = new Size(50, 50);
        Color c = Color.FromName("black");

       //Pass the position x to the block
        public Block(Point x)
        {
            _x = x;
            Screen = new Panel
            {
                Size = s
            };
        }
        public Panel _ScreenPanel()
        {
            Screen.Location = _x;
            return Screen;
        }

        public void  SpawnBlock()
        {
            Box = new PictureBox
            {
                Dock = DockStyle.Fill,
                Name = "Obstacle" + _x,
                BorderStyle = BorderStyle.FixedSingle,
                BackColor = c
            };
           Screen.Controls.Add(Box);
        }
    }