Space images in an array out evenly, taking into account different image widths
Question
local xOffset = 0
for i = 1, levelPacks[prevCurrentLevelPack][prevCurrentLevel].ammount do
if i == 1 then --setup first one
shapesPrevArray[i].x = 30
shapesPrevArray[i].y = 41
shapesPrevArray[i].isVisible = true
end
if i > 1 then --setup the rest
--width of previous one plus the x value of the previous one to make them next to eachother.
xOffset = shapesPrevArray[i - 1].width + shapesPrevArray[i - 1].x
print("offset: " .. xOffset)
shapesPrevArray[i].x = xOffset
shapesPrevArray[i].y = 41
shapesPrevArray[i].isVisible = true
xOffset = 0
end
i = i + 1
end
i:2 width:60 x value:30 xoffset:90
i:3 width:40 x value:90 xoffset:130
i:4 width:50 x value:130 xoffset:180
i:5 width:70 x value:180 xoffset:250
Can anyone help me out please? I can't work out why this bit isn't spacing them out.
xOffset = shapesPrevArray[i - 1].width + shapesPrevArray[i - 1].x
If someone could explain why it isn't and point me in the right direction I would be grateful.
Thanks.
Solution
You should remove i = i + 1
A numeric for has the following syntax:
for var=exp1,exp2,exp3 do something end
That loop will execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression is optional; when absent, Lua assumes one as the step value.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow