How to add event to duplicate item?(solved)

Hi guys, iv been trying corona for a few days now and all is great, however i have an issue i cant seem to figure out.

What i basically want to do is to have several crates within my scene and apply the same ‘drag’ even to each of them. I have managed to create several crates from a loop but im unsure how id go about add the same event to each? Can anybody point me in the right direction?

Also iv noticed that around each crate is a 1 pixel invisible border, when you stack the crates this is clearly obvious. any ideas as to what is going on?

Thanks very much :slight_smile: [import]uid: 162458 topic_id: 28987 reply_id: 328987[/import]

In the loop you use to create the crates, you can simply just add the same event listener to each crate that calls the same function. You would only need to add the “drag” function once somewhere in your code for it to be called by each crate.
You could also add an “id” value to each crate so that in your “drag” function you could see which crate is being dragged at that time.

In regards to the invisible border, thats probably because the image you created doesn’t fill up the whole of its canvas. If you open the image in photoshop and make sure its goes up to the images border then it should be fine. Alternatively you could create a custom physics shape for the crate that is slightly smaller than the image to make it work correctly.

Most of that probably made little sense :smiley: so i will elaborate further if needed.

[import]uid: 69826 topic_id: 28987 reply_id: 116653[/import]

Thank you very much for the reply. I understand the principle with regards to the event, however as i used an example to create the drag event, im unsure if its correct to apply to each crate…do you have an example how i would acheive adding the same event to each crate?

The image fills the border when opened up in photshop, How would i create a custom shape?

Sorry for all the beginners questions but thanks for taking the time to reply. [import]uid: 162458 topic_id: 28987 reply_id: 116655[/import]

This is my code so far…

i create an array of crates and try to add the listener but fails

[code]local crates = {}

for i = 1, 5 do
for j = 1, 5 do
crates[i] = display.newImage( “crate.png”, 140 + (i*50), 220 - (j*50) )
physics.addBody( crates[i], { density=0.2, friction=0.1, bounce=0.5 } )
crates[i]:addEventListener(“touch”, listener(crates[i]))
end
end [/code]

and my drag event

[code]local function listener(crate)
return function(event)
local crate = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( crate )

– Store initial position
crate.x0 = event.x - crate.x
crate.y0 = event.y - crate.y

– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”

– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

– Start tracking velocity
–Runtime:addEventListener(“enterFrame”, trackVelocity(crate))

else
if “moved” == phase then
crate.x = event.x - crate.x0
crate.y = event.y - crate.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
event.target.bodyType = “dynamic”
–crate:setLinearVelocity(speedX, speedY)

–Runtime:removeEventListener(“enterFrame”, trackVelocity(crate))
end
end

– Stop further propagation of touch event!
return true
end
end[/code] [import]uid: 162458 topic_id: 28987 reply_id: 116675[/import]