help !! about table of image.load

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
justatest
Posts: 10
Joined: Mon Dec 19, 2005 5:56 pm

help !! about table of image.load

Post by justatest »

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 !
mrn
Posts: 116
Joined: Wed Nov 02, 2005 2:26 am

Post by mrn »

are you filling your Block_Pos in the main routine outside any function?
Then it is Global. You should fill it before using it.

If first you try to use it inside a function without filling it before ...
ya, i think this is the prblm here..
justatest
Posts: 10
Joined: Mon Dec 19, 2005 5:56 pm

Post by justatest »

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 . ;)
nevyn
Posts: 136
Joined: Sun Jul 31, 2005 5:05 pm
Location: Sweden
Contact:

Post by nevyn »

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}, 
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Indexing is not the problem since index variables all the time.
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 
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 :-)
Post Reply