Tables...aways tables.....

Sorry about my english.

I can't find my error.

Im improving my game and I use themes to change graphics.

I store the graphics names like this:

enemy_list = {"2players.png","player-ia1.png","player-ia2.png"}

So, when I will use this graphics, I just use something like that:

enemy_pic = display.newImageRect("image/"..theme.."/"..enemy_list[player_num], 320, 150)

where THEME is the image directory, and enemy_list is the table above.

My problem is: When I print these values BEFORE call display.newImageRect I have this values:

player_num 1
enemy_list[1] 2players.png
enemy_list[2] player-ia1.png
enemy_list[3] player-ia2.png

BUT, enemy_list[player_num] (using the player_num variable)aways becomes nil

What Im doing wrong?

Thanks for any help!

Perhaps player_num is exceeding the array? Does player_num ever become greater than 3?

Maybe enemy_list is not visible when you call display.newImageRect()

Make sure the table is in scope, maybe try declaring it right before

enemy_pic = display.newImageRect("image/"..theme.."/"..enemy_list[player_num], 320, 150)

Thanks for your help.

No, player_num NEVER is bigger than 3. And the table and the variables is in the same scope. Thats why I dont understand the error....

Im using director class, but all is inside the same function and same scope.

The values I wrote is in the code. If I print (player_num) always have a number. If I print enemy_list[3] (or other number), I aways have the right value. But if I use togheter, I receive nil.


How are you assigning a value to player_num?

Could it be that player_num is "3" (as a string) instead of 3 (a number)?

You could try something like this:

enemy_pic = display.newImageRect("image/"..theme.."/"..enemy_list[ tonumber ( player_num ) ], 320, 150)

You can check the type of player_num using type():

print( type( player_num ) )

make sure it is assigned a number

hope it helps

Raúl Beltrán
MIU Games



That was EXACTLY the problem. When I ran the program the first time, everything worked, so I was confused. The variable had a number in it.

When closing the program, this value is written to a file and is read by the program and of course, was loaded as a text. I was not able to see the error.

Thanks for the help.

Glad I could help :)

Raúl Beltrán
MIU Games

views:1004 update:2011/10/17 8:58:49
corona forums © 2003-2011