【问题标题】:Place objects in tablelayoutpanel cells in specific order按特定顺序将对象放置在 tablelayoutpanel 单元格中
【发布时间】:2014-10-24 16:54:57
【问题描述】:

我目前被困在我似乎无法弄清楚的部分任务中。它涉及我设置一个游戏板,使用一个 tablelayoutpanel 作为游戏板。

到目前为止,我已经在板上显示了“正方形”图块,但是图块(单元格)的顺序不是这样:

我需要在左下角显示开始磁贴,在右上角显示结束,中间的单元格按如下顺序显示:

与这部分相关的方法如下:

/////此方法使用“正方形”数组设置游戏板,(仍在进行中,但大部分已完成)

private void SetupGameBoard() {

                    for (int i = 0; i <= 41; i++) {

            SquareControl squareCreate = new SquareControl(Board.Squares[i], null);

            int coloumn = 0;
            int row = 0;

            MapSquareNumToScreenRowAndColumn(i, out coloumn, out row);

            boardTableLayoutPanel.Controls.Add(squareCreate, coloumn, row);

        }


    }// SetupGameBaord

还有这个,

 private static void MapSquareNumToScreenRowAndColumn(int squareNumber, out int rowNumber, out int columnNumber) {



            // ######################## Add more code to this method and replace the next two lines by something more sensible.  ###############################
            rowNumber = 0;      // Use 0 to make the compiler happy for now.
            columnNumber = 0;   // Use 0 to make the compiler happy for now.                  



        }//end MapSquareNumToScreenRowAndColumn

到目前为止,我所掌握的是,在 MapSquareToScreenRowandcColumn 中,我需要进行计算来确定正方形的位置。

真的很想得到这方面的一些提示/建议/帮助/任何东西。

如果你们需要更多信息,请告诉我。

谢谢!

编辑:

所以我想出了一种方法,只是想知道一种更好的方法。在 MapSqauretoScreenRowandColumn 我放了这个小 if 语句只是为了测试:

if (squareNumber >= 0 && squareNumber <= 5) {

                rowNumber = 6;


            }

因此,如果 sqaurenumber 大于 0 且小于 5,则将其放置在右侧。我的问题是,有没有更好的方法来做这件事?而不是为此写一堆行,有没有办法在几行中做到这一点?

【问题讨论】:

    标签: c# tablelayoutpanel


    【解决方案1】:

    您的参数需要行,然后是列

    private static void MapSquareNumToScreenRowAndColumn(int squareNumber,
                                        out int rowNumber, out int columnNumber) {
    

    但你有它倒退:

    MapSquareNumToScreenRowAndColumn(i, out coloumn, out row);
    

    【讨论】:

    • 感谢您的回复,但我放置它们的顺序真的很重要吗?我认为这与 MapSquareNumtoScreen(etc..) 中的实际计算有关。
    • @user3496101 我在您的代码中看不到您是如何计算列和行值的。但我的回答是处理您混淆列和行的事实。我认为只有两个循环会更容易:fox y = 0 to rows and then for x = 0 to columns 在里面。
    【解决方案2】:

    我可以给你一个打击,但我不能告诉你一切,因为我在同一个班级。哈哈。

    基本上:

    坐标 (x,y) 与 (columnNumber,rowNumber) 相同,注意不要混淆这些。其中 columnNumber = x 和 rowNumber = y。

    现在要计算出我们所在的 x 和 y,我们只需做简单的数学运算。

    Table size: (6,7) (6 wide by 7 high),
    

    我们可以根据索引计算出x和y是什么。

    让我们从 x 开始:

    要解决 x 是什么,我们需要做的就是找出在任何给定时间它有多少。很简单:

    x = index % table.width
    

    现在我们求解 y 这要求我们从最大值倒数到最小值。所以:

     y = table.height - (index/table.width)
    

    我们还用 int 进行除法,这样小数就落在末尾,这是我们在这种情况下想要的。 我们除以宽度,因为我们想计算出它经过了多少列才能到达当前位置。

    现在要获得反转效果,我们基本上希望每个奇数列都发生这种情况 所以我们需要做的基本上是:

     if (y % 2 != 0) { //if it is an odd number
        x = table.width - x;
     }
    

    创建反向效果;

    另外,请记住 KEY POINT、table.width 和 table.height = 键,所以如果 table.width = 6 则键为 5,因为数组从 0 开始...

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2016-03-22
      • 1970-01-01
      相关资源
      最近更新 更多