How can I address dynamically created display objects?

Hi there,

I’m creating a simple 2D puzzle game as a means to learn Corona.

The game involves a 10x10 grid of individual rectangle objects which I have created dynamically based on a table (that has other value in it such as .x, .y, ‘name’.

One value in the table is a unique text string such as [name= “object27”] - there are 100, one each for each of the rectangle display objects I subsequently create.

I retrieve this name value and use it to create each of the 100 display objects as follows:

for i = 1, (numOfColumns*numOfRows) do

local object = table[i].name – get string to use to create object name e.g. “object27”
object = display.newRect( x, y, 80, 80 )
objectGroup:insert( object )

Now I can

  • draw all 100 rectangles
  • retrieve objectGroup.numChildren (is 100 as expected)
  • print ("object name is " … object) ( shows “object name is object27” etc etc)

But when I try to address any of the objects individually (e.g. to change it’s .x value) the console tells me…Runtime error…attempt to index global ‘object27’

I cant see where I am going wrong. Any help appreciated.

:slight_smile: [import]uid: 191070 topic_id: 35290 reply_id: 335290[/import]

I’m new to Corona myself as well but I think your problem lies in how you attach your touch listener.

You lose your pointer to the newly created object, because you create it with local keyword and then insert it in your display group and bam, your “object” pointer is long gone.

There are several ways to prevent it, simplest way would be so have a table and store all these in that table as well so you can later on use that table to retrieve your objects. Note that this way is called “Dictionary” because your indexes are not numbers, but strings.

So somewhere before end of your for loop, like just before the “end”, you could write something like this:

myDictionary[object.name] = object

so you could later on read from this via:

local tempObect = myDictionary[object.name]

Disclaimer: As I’ve said before, I’m new to Lua and Corona as well, so guys please correct me if I’m wrong, wrote this based on my coding experience writing games for other platforms. [import]uid: 206803 topic_id: 35290 reply_id: 140277[/import]

You can define your object as table and call by their index. Assuming you know your object27 is at row 27 of your table which has x and y values defined.

local object = {}  
for i = 1, (numOfColumns\*numOfRows) do  
 object[i] = display.newRect( table[i].x, table[i].y, 80, 80 )  
 object[i].name = table[i].name  
 objectGroup:insert( object[i] )  
  
end  

then move your object

object[27].x = 200  
object[27].y = 150  

burhan [import]uid: 74883 topic_id: 35290 reply_id: 140284[/import]

kevin,

I’d spawn your “objects” into a table. I like to do it like this FWIW.

Plug & Play code… this should give you a lot of ideas.

local numOfColumns = 10   
local numOfRows = 10  
  
local t = {}  
t[1] = {}  
t[1].x = 200  
t[1].y = 200  
t[1].name = "Object1"  
t[1].color = "BLUE"  
--etc, you get the idea here how to build the rest of the t thru [100]  
  
local Object = {}  
local ObjectGroup = display.newGroup()  
  
local function printMyName(event)  
 local MyName = event.target.name  
 i = event.target.ID  
  
 if event.phase == "ended" then  
 print("Here are several ways to reference an Object spawned into a table!")  
  
 print("My Name is "..MyName.. " so you can track me.")  
  
 print("My Name is Object"..i )  
  
 print("Another way to call My Name is "..Object[i].anotherName )  
  
 print("My Color is ",Object[i].color)  
 end  
 return true  
end  
local function spawnObjects()  
 --for i = 1, (numOfColumns\*numOfRows) do --commented out because my table only has index [1]  
 for i = 1, #t do  
 Object[i] = display.newRect(0, 0, 80, 80)  
 Object[i].x = t[i].x  
 Object[i].y = t[i].y  
 Object[i].name = t[i].name  
 Object[i].color = t[i].color  
 Object[i].ID = i  
 Object[i].anotherName = "Object"..i..""  
 Object[i]:addEventListener("touch", printMyName)  
 ObjectGroup:insert(Object[i])  
 end  
end  
  
spawnObjects() --calls function on load to spawn the Objects  
  

If you spawn your Objects into a table you can easily call, modify and reference the Object later.
I always give the spawned Object and “ID” that matches it’s spawned index number reference.

@Aiden, this is what I was getting at in the other thread.

Hope this helps,

Nail
[import]uid: 106779 topic_id: 35290 reply_id: 140285[/import]

If the mark of a good technology/tool is how helpful and passionate it’s community of users is then Corona has a very bright future!

Many thanks guys - I’m experimenting now. [import]uid: 191070 topic_id: 35290 reply_id: 140310[/import]

I’m new to Corona myself as well but I think your problem lies in how you attach your touch listener.

You lose your pointer to the newly created object, because you create it with local keyword and then insert it in your display group and bam, your “object” pointer is long gone.

There are several ways to prevent it, simplest way would be so have a table and store all these in that table as well so you can later on use that table to retrieve your objects. Note that this way is called “Dictionary” because your indexes are not numbers, but strings.

So somewhere before end of your for loop, like just before the “end”, you could write something like this:

myDictionary[object.name] = object

so you could later on read from this via:

local tempObect = myDictionary[object.name]

Disclaimer: As I’ve said before, I’m new to Lua and Corona as well, so guys please correct me if I’m wrong, wrote this based on my coding experience writing games for other platforms. [import]uid: 206803 topic_id: 35290 reply_id: 140277[/import]

You can define your object as table and call by their index. Assuming you know your object27 is at row 27 of your table which has x and y values defined.

local object = {}  
for i = 1, (numOfColumns\*numOfRows) do  
 object[i] = display.newRect( table[i].x, table[i].y, 80, 80 )  
 object[i].name = table[i].name  
 objectGroup:insert( object[i] )  
  
end  

then move your object

object[27].x = 200  
object[27].y = 150  

burhan [import]uid: 74883 topic_id: 35290 reply_id: 140284[/import]

kevin,

I’d spawn your “objects” into a table. I like to do it like this FWIW.

Plug & Play code… this should give you a lot of ideas.

local numOfColumns = 10   
local numOfRows = 10  
  
local t = {}  
t[1] = {}  
t[1].x = 200  
t[1].y = 200  
t[1].name = "Object1"  
t[1].color = "BLUE"  
--etc, you get the idea here how to build the rest of the t thru [100]  
  
local Object = {}  
local ObjectGroup = display.newGroup()  
  
local function printMyName(event)  
 local MyName = event.target.name  
 i = event.target.ID  
  
 if event.phase == "ended" then  
 print("Here are several ways to reference an Object spawned into a table!")  
  
 print("My Name is "..MyName.. " so you can track me.")  
  
 print("My Name is Object"..i )  
  
 print("Another way to call My Name is "..Object[i].anotherName )  
  
 print("My Color is ",Object[i].color)  
 end  
 return true  
end  
local function spawnObjects()  
 --for i = 1, (numOfColumns\*numOfRows) do --commented out because my table only has index [1]  
 for i = 1, #t do  
 Object[i] = display.newRect(0, 0, 80, 80)  
 Object[i].x = t[i].x  
 Object[i].y = t[i].y  
 Object[i].name = t[i].name  
 Object[i].color = t[i].color  
 Object[i].ID = i  
 Object[i].anotherName = "Object"..i..""  
 Object[i]:addEventListener("touch", printMyName)  
 ObjectGroup:insert(Object[i])  
 end  
end  
  
spawnObjects() --calls function on load to spawn the Objects  
  

If you spawn your Objects into a table you can easily call, modify and reference the Object later.
I always give the spawned Object and “ID” that matches it’s spawned index number reference.

@Aiden, this is what I was getting at in the other thread.

Hope this helps,

Nail
[import]uid: 106779 topic_id: 35290 reply_id: 140285[/import]

If the mark of a good technology/tool is how helpful and passionate it’s community of users is then Corona has a very bright future!

Many thanks guys - I’m experimenting now. [import]uid: 191070 topic_id: 35290 reply_id: 140310[/import]