使用数组表格创建对象

我试图通过CE Lua脚本在我的表格中制作几个面板对象。如何以正确的方式做到这一点?。

  local bricks = {}
  local brickWidth = 70
  local brickHeight = 25
  local brickRows  = 6
  local brickColumns  = 6

  local rleft = 5
  local rtop = 5
  local cleft = 5
  local ctop = 10

  for row = 0, brickRows do
   for column = 0, brickColumns do
   bricks[row] = createPanel(gameMain)
   bricks[row].Width = brickWidth
   bricks[row].Height = brickHeight
   bricks[row].Top = rtop
   bricks[row].Left = rleft
   bricks[row].Color = math.random(10,65255)
   rleft = rleft + brickWidth + 5
   bricks[column]  = createPanel(gameMain)
   bricks[column].Width = brickWidth
   bricks[column].Height = brickHeight
   bricks[column].Left = cleft
   bricks[column].Top = brickHeight + 5
   bricks[column].Color = math.random(10,65255)
   ctop = ctop + brickHeight + 5
   end
  end

但它失败了。我想要的是每个行和列都包含6个面板。如何编写正确的脚本?谢谢

点赞
用户2858170
用户2858170

创建一个包含所有砖块的表格。

每行创建1个表格。

在每一行中创建并添加1个砖块到每个列中。

简单使用循环计数器来计算偏移量。

也许你应该先用笔和纸解决这样的问题。

local rows, cols = 6, 6
local width, height = 70, 25
local gap = 5

local bricks = {}

for row = 1, rows do
  bricks[row] = {}
  for col = 1, cols do
    local x = (col - 1) * (width + gap) -- x 偏移量
    local y = (row - 1) * (height + gap) -- y 偏移量
    local newBrick = createPanel(gameMain)
    -- 分配砖块的属性
    -- ...
    bricks[row][col] = newBrick
  end

end
2019-07-11 07:54:44