Dragging a dynamically created object

Hello,

I have a list of images created dynamically via a loop and appended to table. My question is how can I add the dragging touch event listener described here: http://coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/ to table items?

Specifically this part: function myObject:touch( event )… how can I have it appended to items in my table?

Thanks.

Add an index ID to your objects when you spawn them. 

MyObject[i].ID = i

then in your touch listener, something like this.

MyObject[event.ID].x = event.x

Just remember not to destroy individual indexed objects until you can destroy ALL of them at the end of the scene or round or whatever.  If you remove them from the table, the new objects index won’t match up with the spawned ID.  Just make them MyObject[event.ID].isVisible = false.

Hope this helps,

Nail

If you are creating images in a loop you should not add them to a table but put them into a display group. As each image is created, attach the touch (or other) listener directly to it just as you would any other object.

I don’t really understand.

Do you mean like this:

letterImgTbl[i]:addEventListener( “touch”,

function ( event )

   if event.phase == “began” then

       self.markX = self.x    – store x location of object

       self.markY = self.y    – store y location of object

   elseif event.phase == “moved” then

       local x = (event.x - event.xStart) + self.markX

       local y = (event.y - event.yStart) + self.markY

       

       self.x, self.y = x, y    – move object based on calculations above

   end

   

   return true

end 

)

That is giving: Attempt to index global ‘self’ (a nil value)

Please clarify.

Sort of. I would define the touch function completely separately. Then you can attach it to each object in the loop like this:
letterImgTbl[i]:addEventListener( “touch”, touch )

You would have the touch function defined like this:
local function touch( event )

Btw, in the touch function you would not necessarily use ‘self’ (I rarely do) but I would use:
local self = event.target

Thanks guys, I now have the complete picture. The source of the confusion was me thinking the self is a built in the event listener that is automagically added!

Now it works fine.

Add an index ID to your objects when you spawn them. 

MyObject[i].ID = i

then in your touch listener, something like this.

MyObject[event.ID].x = event.x

Just remember not to destroy individual indexed objects until you can destroy ALL of them at the end of the scene or round or whatever.  If you remove them from the table, the new objects index won’t match up with the spawned ID.  Just make them MyObject[event.ID].isVisible = false.

Hope this helps,

Nail

If you are creating images in a loop you should not add them to a table but put them into a display group. As each image is created, attach the touch (or other) listener directly to it just as you would any other object.

I don’t really understand.

Do you mean like this:

letterImgTbl[i]:addEventListener( “touch”,

function ( event )

   if event.phase == “began” then

       self.markX = self.x    – store x location of object

       self.markY = self.y    – store y location of object

   elseif event.phase == “moved” then

       local x = (event.x - event.xStart) + self.markX

       local y = (event.y - event.yStart) + self.markY

       

       self.x, self.y = x, y    – move object based on calculations above

   end

   

   return true

end 

)

That is giving: Attempt to index global ‘self’ (a nil value)

Please clarify.

Sort of. I would define the touch function completely separately. Then you can attach it to each object in the loop like this:
letterImgTbl[i]:addEventListener( “touch”, touch )

You would have the touch function defined like this:
local function touch( event )

Btw, in the touch function you would not necessarily use ‘self’ (I rarely do) but I would use:
local self = event.target

Thanks guys, I now have the complete picture. The source of the confusion was me thinking the self is a built in the event listener that is automagically added!

Now it works fine.