How to change value in map table?
Posted: Mon Aug 01, 2022 10:22 pm
Recently I started learning love2d and now I am making a collision map in tiles.
I want to change only index where some random sized boxes generated in the map table by loop. But somehow I can't get them changed correctly. I am quite a beginner and have no clear understang how tables work.. Could someone give me correct way to do it? currently I set mapsize width 480, height 320, and tilesize is 16.
I want to change only index where some random sized boxes generated in the map table by loop. But somehow I can't get them changed correctly. I am quite a beginner and have no clear understang how tables work.. Could someone give me correct way to do it? currently I set mapsize width 480, height 320, and tilesize is 16.
Code: Select all
function generateBoxes()
for i=1,5 do
local x=math.random (0, mapsizex*tilesize)
local y=math.random (0, mapsizey*tilesize)
local width=math.random(1,6)
local height=math.random(1,4)
ChangeMap(math.floor(x/tilesize),math.floor(y/tilesize),width,height,1)
end
PrintMap()
end
function MakeNewMap()
for row = 1,mapsizey do
map[row] = {}
for column = 1,mapsizex do
map[row][column]= 0
end
end
end
function ChangeMap(x,y,w,h,num)
print("x"..x.." y"..y.." w"..w.." h"..h)
for i=y, h+1 do
for j=x,w do
map[i][j]=num
end
end
end
function PrintMap()
for i = 1, #map do
local s = ''
for j= 1, #map[i] do
s = (s .. ' ' .. map[i][j])
end
print(s)
end
end