onCollision?

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] [import]uid: 53149 topic_id: 13146 reply_id: 313146[/import]

do you mean this?

number\_1\_tile[i].collision = onCollision number\_1\_tile[i]:addEventListener ("collision", number\_1\_tile[i]) [import]uid: 55057 topic_id: 13146 reply_id: 48225[/import]

@ricebowl1988 - I tried that… still got the error…
Runtime error
…orking_title_game/_lab_working_title/lab_b/match.lua:160: attempt to index field ‘?’ (a nil value)
Here’s the full code:

[lua]local number_2_tile = {}

local function number_2_T ()
for i = 1, 18 do

number_2_tile[i] = display.newImage(“images/number_2_tile.png”, true)
number_2_tile[i].x = math.random(80, 680)
number_2_tile[i].y = math.random(80, 980)
physics.addBody(number_2_tile[i], tilePhysics)
number_2_tile[i]:addEventListener(“touch”, dragBody)
tileGroups:insert(number_2_tile[i])
number_2_tile[i].name =“number_2_tile[” … i … “]”
end
end
number_2_T ()

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])
– OR
number_1_tile[" … i … “].collision = onCollision
number_1_tile[” … i … “]:addEventListener (“collision”, number_1_tile[” … i … "])[/lua]

I get an error with either. I need for each of the generated elements to detect a collision hence [i].

Thanks for any help!

[import]uid: 53149 topic_id: 13146 reply_id: 48229[/import]

Well first of all, number_1_tile was never made into a table, like you did with number_2_tile = {} at the top of the code. So you’re trying to access that variable as a table when Lua doesn’t know it suppose to be a table.

Secondly, you are confusing making a string that has the value “number_1_tile[3]” and actually referencing number_1_tile[3].

The reason why you are using the “number_1_tile[” … i … “]” is you are creating a text “name” for that object so you can ID it later when it comes into an event handler as "event.other… " object. BTW: I think its .myName not .name…

Any way, when you get to the last two lines, you’re not making a string any more, you’re wanting to actually access the object at number_1_tile[i]. So the last two lines have to be:

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

[import]uid: 19626 topic_id: 13146 reply_id: 48249[/import]

@robmiracle - I meant to put:

[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[i].myName =“number_1_tile[” … i … “]”

end

end

number_1_T();[/lua] [import]uid: 53149 topic_id: 13146 reply_id: 48257[/import]

I am still getting the error despite the instructions in your explanation:

[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)
number_1_tile[i].myName =“number_1_tile[” … i … “]”

end

end

number_1_T();

local number_2_tile = {}

local function number_2_T ()
for i = 1, 18 do

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


– COLLISION

local function onCollision ( self, event )
if(self.name == “ply1Ring” and event.other.name == “number_1_tile[i]”) 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] [import]uid: 53149 topic_id: 13146 reply_id: 48258[/import]

What is line 160?

What is the value of “i” before the end?

Regardless the last two lines will only setup the event listener on whatever value “i” is currently set too. You need a for loop to loop over the table to assign listeners to each one.
[import]uid: 19626 topic_id: 13146 reply_id: 48260[/import]

@robmiracle - So thank you for the knowledge you shared. I am learning. I need to understand for loops better and how it relates to collision.

Here’s the working code…in case anyone else was following this post. Again many thanks to you and everyone that helped me out.

[lua]local number_1_tile = {}

local function number_1_T ()
for i = 1, 2 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)
number_1_tile[i].name =“number_1_tile[” … i … “]”
–number_1_tile[i].name = “tilesOne”


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])

end – Ends for loop

end – ends fnction

number_1_T();[/lua] [import]uid: 53149 topic_id: 13146 reply_id: 48371[/import]

I would hope Corona SDK/Lua will optimize this, but your:

local function onCollision ( self, event )

is inside your for loop, and it probably should be defined before the loop.

But you are off to a good start! [import]uid: 19626 topic_id: 13146 reply_id: 48409[/import]

See I thought it should be. I am not sure why, but when I take it outside the for loop. It no longer sees number_1_tile[i]… It’s like it was never declared. I stuck inside the for loop where I know it gets declared. I saw something about variable forwarding… In Flash it does not matter where you’re funtions are in relation to when you call them. LUA/Corona is different.

I am moving forward. Here’s another question. Something I was curious about using the same code. How do I go about loading different tiles using the same for loop? [import]uid: 53149 topic_id: 13146 reply_id: 48529[/import]

Are you wanting to say have objects like number_1_tile, number_2_tile, number_3_tile where you have tiles numbered 1 to 9 and you want multiple tiles of each type.

If so I would do something like:

[lua]localGroup = display.newGroup()
tiles = {}

local function onCollision(event)
– TBD
end

local function makeTile(tileType)
local tile = display.newImage(“images/number_” … tileType … “_tile.png”, true)
tile.x = math.random(80, 680)
tile.y = math.random(90, 980)
tile.tileType = tileType
physics.addBody(tile,tilePhysics)
tile:addEventListener(“touch”, dragBody)
tile.myName = “tile_” … tileType
tile:addEventListener (“collision”, onCollision)
return tile
end

– now spawn each individual tile
– to do say 10 of the same tile:

for i = 1, 10 do
tiles[i] = makeTile(1)
tiles[i].myName = tiles[i].myName … “_” i
tiles[i].index = i
localGroup:insert(tiles[i])
done

– spawn 10 random numbered tiles

for i = 1, 10 do
tiles[i] = makeTile(math.random(9))
tiles[i].myName = tiles[i].myName … “_” i
localGroup:insert(tiles[i])
done[/lua]

Now what is supposed to happen when a tile collides with the player? [import]uid: 19626 topic_id: 13146 reply_id: 48585[/import]

@robmiracle - you make it seem too easy. I am actually trying to read up on it and looking at the various implementations. As this method is preferred over manually creating each individual object. Hey, this is great. Thanks

To answer you question: The players are dragging the tiles into one of two rings. Each tile has a value associated with it. Player with the most tiles wins. Simple game while I learn this corona thing. I am a designer trying to figure out this coding thing :slight_smile: [import]uid: 53149 topic_id: 13146 reply_id: 48715[/import]