C# 实例共享相同的值

C# Instances share same values

这里是编程专业的学生,​​对我要问的问题很陌生,但我相信你们会知道的。我必须制作一个游戏,其中使用数组创建多个图片框。我还必须制作一个健康变量为 5 的 class。当您单击其中一个图片框时,它的健康状况必须下降 1。我做到了这一点,但问题是健康变量由所有图片框共享,实际上我希望每个图片框都有自己的健康状况。

这是我的代码:

public partial class Form1 : Form
{

    Invader monster; // Invader is the name of the class
    Random rand = new Random();
    PictureBox[] pb = new PictureBox[5];

    private void Spawner()
    {
        for (int i = 0; i < 5; i++)
        {
            this.monster = new Invader();
            this.pb[i] = new PictureBox();
            this.pb[i].Name = "pb" + i.ToString();
            this.pb[i].Location = new Point(rand.Next(10, 300), monster.LocY);
            this.pb[i].BackgroundImageLayout = ImageLayout.Stretch;
            this.pb[i].BackgroundImage = Image.FromFile(@"Path");
            this.pb[i].BackColor = Color.Transparent;
            this.pb[i].Size = new System.Drawing.Size(40, 30);
            this.Controls.Add(this.pb[i]);
            this.pb[i].Click += this.Form1_Click;
        }
    }

    private void Form1_Click(object sender, EventArgs e)
    {
        PictureBox currentpicturebox = (PictureBox)sender;

        this.monster.HealthDown();
        if (this.monster.Health == 0)
        {
            currentpicturebox.Dispose();
        }

    }

和我的 class:

class Invader
{
    // Fields
    private int health;

    // Properties
    public int Health
    {
        get { return this.health; }
    }

    // Constructor
    public Invader()
    {
        this.health = 5;
    }
    // Methods
    public void HealthDown()
    {
            this.health -= 1;
    }

假设我点击 1 个图片框 4 次,然后点击另一个图片框 1 次。使用此代码,最后单击的图片框将被丢弃。关于如何解决此问题的任何想法?

您的 Invader monster 是 Form1 的一个实例变量,在您的方法 Spawner() 中,您每次都在 for 循环内重新分配它:this.monster = new Invader();

基本上,当你在你的 Form1_Click 方法中点击一个图片框(没有什么不同)时,每次你的最后一个怪物 istance 使它的健康下降而不是预期的。

要解决此问题,您可以:

  • 在Invader对象数组中转换怪物而不是Invader对象,元素数量必须与图片框数量相同
  • foreach picturebox 将怪物数组中对应的 Invader 的索引作为 Tag 分配

举个例子:

public partial class Form1 : Form
{
    // EDIT - Become an array
    Invader[] monster = new Invader[5]; // Invader is the name of the class
    Random rand = new Random();
    PictureBox[] pb = new PictureBox[5];

    private void Spawner()
    {
        for (int i = 0; i < 5; i++)
        {
            this.monster[i] = new Invader(); // EDIT
            this.pb[i] = new PictureBox();
            this.pb[i].Name = "pb" + i.ToString();
            this.pb[i].Location = new Point(rand.Next(10, 300), monster.LocY);
            this.pb[i].BackgroundImageLayout = ImageLayout.Stretch;
            this.pb[i].BackgroundImage = Image.FromFile(@"Path");
            this.pb[i].BackColor = Color.Transparent;
            this.pb[i].Size = new System.Drawing.Size(40, 30);
            this.Controls.Add(this.pb[i]);
            this.pb[i].Click += this.Form1_Click;
            this.pb[i].Tag = i; // EDIT - Added tag assignation
        }
    }

    private void Form1_Click(object sender, EventArgs e)
    {
        PictureBox currentpicturebox = (PictureBox)sender;
        this.monster[(int)currentpicturebox.Tag].HealthDown(); // EDIT
        if (this.monster[(int)currentpicturebox.Tag].Health == 0) //EDIT
        {
            currentpicturebox.Dispose();
        }
    }