整数保持为0,即使它表示不是

时间:2014-05-05 20:15:25

标签: c# struct int

我正在为我的游戏创建一个关卡设计师,你可以在用户控件上绘制平铺。为了跟踪瓷砖,我做了一个int []:

public int[,] Tiles = new int[35, 35];

因为编辑屏幕是560px乘560px,35个16x16块。 Tiles [x,y] .Id将给出tile的id。

我将是该磁贴的ID。无论何时放置瓷砖,都会在空白处记录:

public void Draw(int x, int y, int bid)
{
    //draw code here
    Program.form.Tiles[x, y].Id = bid;
}

无论何时单击,都会调用void:

//In the MouseClick void:
Draw(e.X / 16, e.Y / 16, 1);

0是可用的块ID,但例如使用1。

每当我去保存它时,(我保存到.txt文件)它应该写下tile的id:

for (int x = 0; x < 35; x++)
{
    for (int y = 0; y < 35; y++)
    {
        //Write code here
        MessageBox.Show(Tiles[x, y].Id.ToString());
    }
}

我只是让它在消息框中显示Id以查看,并且它总是给出'0',即使你在某个x,y平铺上编辑了一个definatley不是0的blockid。 有人知道为什么吗? 谢谢。 顺便说一下,在'Program'类中,在STAThread之上,我把:

public Form1 form;

然后:

form = new Form1();
Application.Run(form);

3 个答案:

答案 0 :(得分:5)

这是因为Tilestruct - 值类型。因此,

Program.form.Tiles[x, y]

给你一份副本。您指定该副本的Id,而阵列中的原始Tile保持不变。这个例子说明了为什么在处理数组或集合中的值类型时应该非常小心。

您可以将Tile更改为class来解决此问题。请注意,与总是具有默认值的值类型(struct s)数组不同,需要手动初始化引用对象数组(class es)的元素以避免&#34;对象引用未设置为对象的实例。&#34;错误:

for (int x = 0; x < 35; x++) {
    for (int y = 0; y < 35; y++) {
        Tiles[x, y] = new Tile();
    }
}

答案 1 :(得分:0)

在您的代码中,您将重新创建重新创建Tiles对象的表单。不要重新创建表单。使用一次:

 public void Draw(int x, int y, int bid)
    {
//If they are in the same form then Program.form is not needed:
        Tiles[x, y].Id = bid;
    }

或者像这样到达你的对象:

 Form1 frm = (Form1)Application.OpenForms[0];

        frm.Tiles[x, y].Id = bid;

答案 2 :(得分:0)

除了可怕的闪烁之外,这应该会很好用。如果我在设计这个,我会使用PictureBox并挂钩其Paint()事件。

public partial class Form1 : Form
{
    Random rnd=new Random();
    int[,] id;
    public Form1()
    {
        InitializeComponent();
    }

    void Place(int x, int y, int bid)
    {
        int i=x/16, j=y/16;
        id[i, j]=bid;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        id=new int[36, 36];
    }

    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);

        int bid=rnd.Next(KnownColor.White-KnownColor.AliceBlue);
        Place(e.X, e.Y, bid);

        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        for(int i=0; i<36; i++)
        {
            for(int j=0; j<36; j++)
            {
                int x=16*i, y=16*j;
                Color color=Color.FromKnownColor(KnownColor.AliceBlue+id[i, j]);
                using(Brush brush = new SolidBrush(color))
                {
                    e.Graphics.FillRectangle(brush, x, y, 16, 16);
                }
                e.Graphics.DrawRectangle(Pens.Black, x, y, 16, 16);
            }
        }

    }

    public string SaveToFile()
    {
        string[] cols=new string[36];
        for(int i=0; i<16; i++)
        {
            string[] rows=new string[36];
            for(int j=0; j<36; j++)
            {
                rows[j]=id[i, j].ToString();
            }
            cols[i]=string.Join(",", rows);
        }
        return string.Join(";", cols);
    }

    public void ReadFromFile(string ids)
    {
        string[] cols=ids.Split(';');
        for(int i=0; i<cols.Length; i++)
        {
            string[] rows=cols[i].Split(',');
            for(int j=0; j<rows.Length; j++)
            {
                int temp_id=0;
                int.TryParse(rows[j], out temp_id);
                id[i, j]=temp_id;
            }
        }
    }
}

Screen

相关问题