I’m trying to write routine that saves a game on application exit and I’ve been stymied by a problem I’ve had with display groups and objects.
I have game graphics defined thus:
[lua]enemy =
{
{ image=“block1.png”, x=20, y=40, yv=yv1, lock=0, val=0, mult=1}
{ image=“block1.png”, x=20, y=40, yv=yv1, lock=0, val=0, mult=1}
– etc
}[/lua]
The enemy table has been inserted into a display group, enemyList:
[lua]local obj = display.newImage(enemy[enemyID].image, enemy[enemyID].x, enemy[enemyID].y)
obj.yv = enemy[enemyID].yv
obj.lock = enemy[enemyID].lock
obj.val = enemy[enemyID].val
obj.mult = enemy[enemyID].mult
obj:addEventListener( “touch”, onTouch )
obj.isVisible = false
enemyList:insert(obj)[/lua]
When I try to save these variables to a file thus…
[lua] dataTable.x1 = enemyList[1].x
dataTable.y1 = enemyList[1].y
dataTable.yv1 = enemyList[1].yv
dataTable.lock1 = enemyList[1].lock
dataTable.mult1 = enemyList[1].mult
dataTable.val1 = enemyList[1].val
dataTable.image1 = enemyList[1].image[/lua]
…everything saves fine, except the last line. enemyList[1].image is nil and I can’t figure out the proper way to read the name of the image file (which may end up as block2.png or something else) used to make the display object.
Is it a scope problem, in that I can’t use a local variable like obj to insert the image and instead have to use a consistent variable throughout the file to make the reference consistent?
Thanks.
[import]uid: 1560 topic_id: 2603 reply_id: 302603[/import]