Hi.
i have been trying to create a app that has a random positioning of certain objects every time
the game loads the same level. The problem is, I get stuck at providing the random coordinates
with the objects. Each object sizes are different, and i need to prevent them from ‘stacking’ on
top of each other.
This is what I get so far:
local gridX = {64, 192, 320, 448, 576, 704, 832, 960} local gridY = {76.8, 230.4, 384, 537.6, 691.2} --loop instantiate holeborder depend on which world local tempX, tempY = {}, {} for i = 1, world do local shapes = tonumber(puzzleCode[i]:sub(4,5)) local colors = tonumber(puzzleCode[i]:sub(1,2)) local levels = tonumber(puzzleCode[i]:sub(7,8)) if world == 1 then print('entering if 1') puzzleCoor[i].x = gridX[math.random(2, 5)] puzzleCoor[i].y = gridY[math.random(1, 3)] else print('entering if else') if levels == 1 then puzzleCoor[i].x = gridX[math.random(2, 5)] puzzleCoor[i].y = gridY[math.random(1, 3)] elseif levels == 2 then puzzleCoor[i].x = gridX[math.random(2, 5)] puzzleCoor[i].y = gridY[math.random(1, 3)] elseif levels == 3 then puzzleCoor[i].x = gridX[math.random(2, 5)] puzzleCoor[i].y = gridY[math.random(1, 3)] elseif levels == 4 then puzzleCoor[i].x = gridX[math.random(1, 6)] puzzleCoor[i].y = gridY[math.random(0, 4)] elseif levels == 5 then puzzleCoor[i].x = gridX[math.random(1, 6)] puzzleCoor[i].y = gridY[math.random(0, 4)] end if tempX[i] ~= 0 and tempY[i] ~= 0 then print('entering loop') for y = 1, #tempX do while math.abs(tempX[y] - puzzleCoor[i].x) \<= gridX[1] do print('shuffle x') if levels == 1 then puzzleCoor[i].x = gridX[math.random(2, 5)] elseif levels == 2 then puzzleCoor[i].x = gridX[math.random(2, 5)] elseif levels == 3 then puzzleCoor[i].x = gridX[math.random(2, 5)] elseif levels == 4 then puzzleCoor[i].x = gridX[math.random(1, 6)] elseif levels == 5 then puzzleCoor[i].x = gridX[math.random(1, 6)] end end end for y = 1, #tempY do while math.abs(tempY[y] - puzzleCoor[i].y) \<= gridY[1] do print('shuffle y') if levels == 1 then puzzleCoor[i].y = gridY[math.random(1, 3)] elseif levels == 2 then puzzleCoor[i].y = gridY[math.random(1, 3)] elseif levels == 3 then puzzleCoor[i].y = gridY[math.random(1, 3)] elseif levels == 4 then puzzleCoor[i].y = gridY[math.random(0, 4)] elseif levels == 5 then puzzleCoor[i].y = gridY[math.random(0, 4)] end end end end tempX[i] = puzzleCoor[i].x tempY[i] = puzzleCoor[i].y print('x', puzzleCoor[i].x, 'y', puzzleCoor[i].y) print('tempX', tempX[i], 'tempY', tempY[i], 'loop', i) end
the reason why I use ‘lines’ is to detect which shape object is being loaded, so that they are not needed to load
out-of-screen, so I random a smaller portion of the table positions