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]