i have make a code like these :
Blocks_Pos = {
["c0"] = {img=Image.load("x0.png"), y=0, x=1},
["c1"] = {img=Image.load("x1.png"), y=2, x=1},
["c2"] = {img=Image.load("x2.png"), y=0, x=0},
["c3"] = {img=Image.load("x3.png"), y=0, x=3},
["c4"] = {img=Image.load("x4.png"), y=2, x=0},
["c5"] = {img=Image.load("x5.png"), y=2, x=3},
["c6"] = {img=Image.load("x6.png"), y=3, x=1},
["c7"] = {img=Image.load("x7.png"), y=3, x=2},
["c8"] = {img=Image.load("x8.png"), y=4, x=0},
["c9"] = {img=Image.load("x9.png"), y=4, x=3}
}
for key in Blocks_Pos do
screen:blit(offsetx+Blocks_Pos[key].x*unit_width, offsety+Blocks_Pos[key].y*unit_height, Block_Pos[key].img)
end
but the visit of Block_Pos[key].img get the err msg as "attempt to index global `Block_Pos' (a nil value)"
so, if i DO want to make a table of image.load , what should i do ? i use luaplayerwindows .
thx guys !
help !! about table of image.load
Moderators: Shine, Insert_witty_name
thx . but seem this is not the problem .
cos i can visit the Block_Pos[key].x and Block_Pos[key].y , but while i change the define as these:
Blocks_Pos = {
{img=Image.load("x0.png"), y=0, x=1, w=2, h=2},
{img=Image.load("x1.png"), y=2, x=1, w=2, h=1},
{img=Image.load("x2.png"), y=0, x=0, w=1, h=2},
{img=Image.load("x3.png"), y=0, x=3, w=1, h=2},
{img=Image.load("x4.png"), y=2, x=0, w=1, h=2},
{img=Image.load("x5.png"), y=2, x=3, w=1, h=2},
{img=Image.load("x6.png"), y=3, x=1, w=1, h=1},
{img=Image.load("x7.png"), y=3, x=2, w=1, h=1},
{img=Image.load("x8.png"), y=4, x=0, w=1, h=1},
{img=Image.load("x9.png"), y=4, x=3, w=1, h=1}
}
all thing work . i think i need more work on lua . ;)
cos i can visit the Block_Pos[key].x and Block_Pos[key].y , but while i change the define as these:
Blocks_Pos = {
{img=Image.load("x0.png"), y=0, x=1, w=2, h=2},
{img=Image.load("x1.png"), y=2, x=1, w=2, h=1},
{img=Image.load("x2.png"), y=0, x=0, w=1, h=2},
{img=Image.load("x3.png"), y=0, x=3, w=1, h=2},
{img=Image.load("x4.png"), y=2, x=0, w=1, h=2},
{img=Image.load("x5.png"), y=2, x=3, w=1, h=2},
{img=Image.load("x6.png"), y=3, x=1, w=1, h=1},
{img=Image.load("x7.png"), y=3, x=2, w=1, h=1},
{img=Image.load("x8.png"), y=4, x=0, w=1, h=1},
{img=Image.load("x9.png"), y=4, x=3, w=1, h=1}
}
all thing work . i think i need more work on lua . ;)
Brackets mean that you index, or take a value out of, an array. This is not what you were trying to do; your code had syntax errors. Just removing the brackets would've worked:
Code: Select all
"c0" = {img=Image.load("x0.png"), y=0, x=1},
Indexing is not the problem since index variables all the time.
try
mixing variable types seems to be your issue make it simpler..
use the for key,value in var loops
remember to combine math functions with parenthesies only to tell it which to do first and which not to.. Yes this LUA does math like a normal person :-)
try
Code: Select all
Blocks_Pos = {
["c0"] = {img=Image.load("x0.png"), y=0, x=1},
["c1"] = {img=Image.load("x1.png"), y=2, x=1},
["c2"] = {img=Image.load("x2.png"), y=0, x=0},
["c3"] = {img=Image.load("x3.png"), y=0, x=3},
["c4"] = {img=Image.load("x4.png"), y=2, x=0},
["c5"] = {img=Image.load("x5.png"), y=2, x=3},
["c6"] = {img=Image.load("x6.png"), y=3, x=1},
["c7"] = {img=Image.load("x7.png"), y=3, x=2},
["c8"] = {img=Image.load("x8.png"), y=4, x=0},
["c9"] = {img=Image.load("x9.png"), y=4, x=3}
}
for key,value in Blocks_Pos do
screen:blit(offsetx+value[x]*unit_width, offsety+value[y]*unit_height, value[img])
end
use the for key,value in var loops
remember to combine math functions with parenthesies only to tell it which to do first and which not to.. Yes this LUA does math like a normal person :-)