Indexing issue? Help?

I am spawning 4 tiles randomly in my game with code below. It works fine, what I am trying to do is the following:

Tile 1 - 4 I want to assign a value too. For example. number_1_tile[1] = 1, number_1_tile[2] = 2, etc… How do I do that?

Maybe the question i need to ask is: How do I access all the elements in an index “local number_1_tile = {}”?

Any help is appreciated. Thanks

[lua]–
local number_1_tile = {}
local n = 0
local function number_1_T ()
for i = 1, 4 do
n = n + 1
number_1_tile[n] = display.newImage(“images/number_1_tile.png”, true)
number_1_tile[n].x = math.random(80, 680)
number_1_tile[n].y = math.random(80, 980)
physics.addBody(number_1_tile[n], tilePhysics)
number_1_tile[n]:addEventListener(“touch”, dragBody)

tileGroups:insert(number_1_tile[n])
number_1_tile[n].name = “number_1_tile”
number_1_tile[1].alpha = .2

end
end

number_1_T()[/lua] [import]uid: 53149 topic_id: 13038 reply_id: 313038[/import]

I’m not certain but as n is obviously counting up and you want each object to be obj[n], or obj[1], obj[2], etc - so why not do obj[n].value = n ?

I haven’t tested it but I believe that should work - let me know, if not I’ll take a better look at it when I’ve had some sleep/coffee :wink: [import]uid: 52491 topic_id: 13038 reply_id: 47970[/import]

Hi spenggong,

You should use the ‘i’ variable in the ‘for loop’ to reference the table elements instead of the ‘n’ variable.

[lua]–
local number_1_tile = {}

local function number_1_T ()
for i = 1, 4 do

number_1_tile[i] = display.newImage(“images/number_1_tile.png”, true)
number_1_tile[i].x = math.random(80, 680)
number_1_tile[i].y = math.random(80, 980)
physics.addBody(number_1_tile[i], tilePhysics)
number_1_tile[i]:addEventListener(“touch”, dragBody)

tileGroups:insert(number_1_tile[i])
number_1_tile[i].name = “number_1_tile”
number_1_tile[1].alpha = .2

end
end

number_1_T()[/lua]

You asked how to assign a value to number_1_tile elements (eg number_1_tile[1] = 1, number_1_tile[2] = 2). If you do this you will overwrite the display.newImage object assigned to the number_1_tile element. I expect what you want to do is set the number_1_tile[].name property to the element number so it can be identified, this is easy to do in the ‘for loop’, for example
[lua]number_1_tile[i].name = “number_1_tile[” … i … “]”[/lua]

I have amended your code to use the ‘i’ variable and set the name property to the table index
[lua]–
local number_1_tile = {}

local function number_1_T ()
for i = 1, 4 do

number_1_tile[i] = display.newImage(“images/number_1_tile.png”, true)
number_1_tile[i].x = math.random(80, 680)
number_1_tile[i].y = math.random(80, 980)
physics.addBody(number_1_tile[i], tilePhysics)
number_1_tile[i]:addEventListener(“touch”, dragBody)

tileGroups:insert(number_1_tile[i])
number_1_tile[i].name = “number_1_tile[” … i … “]”
number_1_tile[1].alpha = .2

end
end

number_1_T()[/lua]

Jon
[import]uid: 26397 topic_id: 13038 reply_id: 48169[/import]

@ Peach Pellen - Thanks [import]uid: 53149 topic_id: 13038 reply_id: 48172[/import]

@ jonlaidler - Yes, exactly! This is Exactly what I was trying to do. Thanks a Trillion… [import]uid: 53149 topic_id: 13038 reply_id: 48177[/import]

@ jonlaidler - why does this not work…

[lua]local function onCollision ( self, event )
if(self.name == “number_1_tile[” … i … “]” and event.other.name == “ply1Ring”) then
p1sum = p1sum + 1
print(p1sum)

end
end

number_1_tile[" … i … “].collision = onCollision
number_1_tile[” … i … “]:addEventListener (“collision”, number_1_tile[” … i … "])[/lua]

What am I doing wrong here? Does this need to be in the onCollision function as well?

[lua]number_1_tile[" … i … “].collision = onCollision
number_1_tile[” … i … “]:addEventListener (“collision”, number_1_tile[” … i … "])[/lua]

Thanks!! [import]uid: 53149 topic_id: 13038 reply_id: 48204[/import]

Hi,

You do not need the line
[lua]number_1_tile[" … i … "].collision = onCollision[/lua]

You add event listeners with object:addEventListener(eventName, listener), eventname is the event to listen for (eg touch, collision etc) and listener being the function to run when the event occurs. So, to add your function onCollision as the function to run when a collision occurs on the first number_1_tile element you would use
[lua]number_1_tile[1]:addEventListener(“collision”, onCollision )[/lua]

The event listener function does not need the Self value in the parameter. Also, you are using a complicated method to referencing the number_1_tile table elements. Each item in a table is referenced by its index (eg number_1_tile[1], number_1_tile[2])
[lua]number_1_tile[1][/lua]

When you want to reference all items in a table you generally use a ‘for loop’ to loop through the table elements. The following will assign the onCollision function to the collision event of all four items in the number_1_tile table
[lua]-- first create your listener function
local function onCollision (event)
– add code to run on collision
print (“collision occurred”)

– access the item calling the function by referencing the event
end

– then assign the onCollision function to the collision event of each element in the number_1_tile table
for i = 1, 4 do
number_1_tile[i]:addEventListener(“collision”, onCollision)
end[/lua]

I am busy at the moment, but will reply this evening in more detail if required.

I guess you are new to programming. Read up on LUA programming, there are a lot of reference sites on the web including some on the Ansca site. Once you understand LUA coding you will find it a lot easier.

Jon [import]uid: 26397 topic_id: 13038 reply_id: 48253[/import]