I’m trying to figure out how to organize my data and identify which object was clicked on from an array.
I have 10 ships in a table, and in the table I have a field called shipGraphic. I attach an event for each of them as I iterate, and they respond when clicked, but I’d like to know which ship was clicked on. Am I making sense?
Here’s my simple code I’m fiddling with. I’d love to have it say “Hey, you clicked on ship 2!”.
[code]
local touchShip = function( event )
if “ended” == event.phase then
print(“We touched a ship!”)
– But I’d like to know which ship out of my array was clicked?
end
end
shipTable = {}
– So we’re making a table of tables…not sure if this is the route to go
for i=1,10 do
– Create our table within our table
shipTable[i] = {}
– Give the ship a name
shipTable[i].name = "Ship: "…i
– Load a graphic into our “shipGraphic” field
shipTable[i].shipGraphic = display.newRect( 0,0,10,20 )
– Place our ship randomly and set a rotation…
shipTable[i].shipGraphic.x = math.random(0,300)
shipTable[i].shipGraphic.y = math.random(0,300)
shipTable[i].shipGraphic.rotation = math.random(0,359)
– Attach the ship graphic to an event
shipTable[i].shipGraphic:addEventListener( “touch”, touchShip )
end
[/code] [import]uid: 11636 topic_id: 4425 reply_id: 304425[/import]
Even if it was by accident! Hahaha!