【问题标题】:Integer stays 0 even when it says it is not整数保持为 0,即使它说它不是
【发布时间】:2014-05-06 05:08:00
【问题描述】:

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

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

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

Id 将是图块的 id。每当放置一个瓦片时,它都会被记录在一个 void 下:

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 磁贴上进行编辑,其 blockid 定义为非 0。 有人知道为什么吗? 谢谢。 顺便说一句,在 STAThread 上面的“程序”类中,我放了:

public Form1 form;

然后:

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

【问题讨论】:

  • 除了Id 之外,Tiles 的成员还有更多吗?否则,只需保留一个 int 数组即可。
  • 试过了,还是 0 @ja72
  • 试过什么?通过编辑问题显示您的代码。
  • 试着把它变成一个 int[]。
  • //In the MouseClick void: 颇有诗意的评论。

标签: c# struct int


【解决方案1】:

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

Program.form.Tiles[x, y]

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

您可以通过将Tile 更改为class 来解决此问题。请注意,与始终具有默认值的值类型数组 (structs) 不同,引用对象数组 (classes) 的元素需要手动初始化以避免“对象引用未设置为一个东西。”错误:

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

【讨论】:

  • 刚把 Tile 改成了一个类,每次点击,它就会出现在 'Program.form.Tiles[x, y].Id = bid;' 行对象引用错误未设置为对象的实例。在课堂上,我也只是将值设置为0,所以这是一个问题。
  • 请提一下,建议struct 具有只读字段,仅在构造函数中设置。否则会发生坏事。
  • 首先你必须创建类的对象,然后你可以给它的成员赋值
【解决方案2】:

在您的代码中,您正在重新创建您的表单,该表单将重新创建您的 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;

【讨论】:

    【解决方案3】:

    这应该工作得很好,除了可怕的闪烁。如果我在哪里设计它,我会使用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;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-25
      • 2021-11-06
      • 2016-08-26
      • 1970-01-01
      • 2017-06-30
      • 2021-04-29
      • 1970-01-01
      • 2013-11-22
      • 2014-01-09
      相关资源
      最近更新 更多