문제

I have this script...

x={300, 450}
y={100, 160}

          ui.addTextArea(2, "<br/><vP><b><p align='center'><a href='event:yes'>Yes</a></b></p>", x[math.random(#x)], y[math.random(#y)];
          ui.addTextArea(3, "<br/><R><b><p align='center'><a href='event:no'>No</a></b></p>", x[math.random(#x)], y[math.random(#y)];
          ui.addTextArea(4, "<br/><VP><b><p align='center'><a href='event:yes'>Yes</a></b></p>", x[math.random(#x)], y[math.random(#y)];
          ui.addTextArea(5, "<br/><R><b><p align='center'><a href='event:no'>No</a></b></p>", x[math.random(#x)], y[math.random(#y)]);

The x and y are the coords for the TextArea, but I always get the same x and y coords for more than 2 textareas. (for example, I get the x=300 and y=100 coords for the 3 and 5 textAreas)

Is there any way to avoid this? Like, once a value is used, it gets skipped and cannot be used/picked anymore.

Not sure how I could explain it better....

도움이 되었습니까?

해결책

Easiest is to use a function that removes item from table when used. Also, unless I'm misunderstanding what you are trying to do, you need as many x and y values as you have text boxes. So try this:

local x = {100, 200, 300, 400, 500} -- five text boxes
local y = {100, 200, 300, 400, 500} -- five text boxes

local function randomExtract(tbl)
    if #tbl > 0 then
      local index = math.random(#tbl)
      return table.remove(tbl, index)
    end
end

ui.addTextArea(1, "<br/>...</p>", randomExtract(x), randomExtract(y))
ui.addTextArea(2, "<br/>...</p>", randomExtract(x), randomExtract(y))
...
ui.addTextArea(5, "<br/>...</p>", randomExtract(x), randomExtract(y))

다른 팁

x={300, 450}
y={100, 160}

xy = {}
for _, X in ipairs(x) do
  for _, Y in ipairs(y) do
    table.insert(xy, {X, Y})
  end
end

ui.addTextArea(2, "<br/><vP><b><p align='center'><a href='event:yes'>Yes</a></b></p>", unpack(table.remove(xy, math.random(#xy))));
ui.addTextArea(3, "<br/><R><b><p align='center'><a href='event:no'>No</a></b></p>",    unpack(table.remove(xy, math.random(#xy))));
ui.addTextArea(4, "<br/><VP><b><p align='center'><a href='event:yes'>Yes</a></b></p>", unpack(table.remove(xy, math.random(#xy))));
ui.addTextArea(5, "<br/><R><b><p align='center'><a href='event:no'>No</a></b></p>",    unpack(table.remove(xy, math.random(#xy))));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top