antikewl
antikewl

Reputation: 51

How do I reference an image that I have created in an table array in Corona (Lua)?

Apologies for the incredibly noob question, but I'm new to Lua, very very rusty at any code, stuck and can't find the solution!

I'm creating a series of random images on screen using:

for count = 1, 6 do
 r = math.random ( 1, 5 )
 mpart[count] = display.newImage ("mpart" .. r .. ".png")
 mpart[count].y = 680
 mpart[count].x = x
 mpart[count].spawnednew = false
 x = x + 170
 mpart[count]:addEventListener ("touch", onTouch)
end

How do I know which object is being touched/moved in the function "onTouch", and how do I add a property to it, e.g.

mpart[1].spawnednew == true

Upvotes: 2

Views: 1037

Answers (2)

jhocking
jhocking

Reputation: 5577

Well first off, lins is spot on about how to reference the touched object: the 'event' parameter of the listener function includes the value 'event.target'

As for adding new data to the touched object, that's as simple as 'event.target.moved = true' and now the object has data at object.moved

Upvotes: 2

lins314159
lins314159

Reputation: 2520

Your onTouch function should have an event parameter passed in. The touched image can then be found by in event.target.

Upvotes: 2

Related Questions