Multiple Drag problem

Hi guys!!, here I'm with another problem while I'm learning to use this espectacular tool.

I have a function A where I can make appear images every three seconds. Also I have another function B that make that image draggable. The function A and B works perfectly when the images made by function A is just one. But, when the function A starts to create images every five seconds, the function B doesn't work, and all the images created by the function A are not draggable.

This is the code:

--***VARIABLES************
local movieclip = require( "movieclip" )
physics.start()
system.activate( "multitouch" )
physics = require("physics")
physics.setGravity(0,9.8)
local baseline = 290
_H = display.contentHeight
_W = display.contentWidth
--************************

--****IMAGE CREATED EVERY 3 SECONDS***********
function newCrate()
rand = math.random( 100 )

if (rand < 70) then
eggBody = { density=1.0, friction=0.1, bounce=0.2, radius=25 }
object1 = movieclip.newAnim{ "shop1.png", "shop2.png" }
object1.x = _W / 2
object1.y = _H / _H - 50
object1.id = "object1"
physics.addBody( object1, eggBody )
object1.isFixedRotation = true
object1.myName = "object1"
end
end
local dropCrates = timer.performWithDelay( 3000, newCrate, 100 )
--*****************************************

--*******************DRAG THE IMAGE CREATED*****************
local function startDrag( event )
local t = event.target

local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y

event.target.bodyType = "kinematic"

event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

if ( not event.target.isPlatform ) then
event.target.bodyType = "dynamic"
end

end
end

return true
end
object:addEventListener( "touch", startDrag )
--************************************************

I ran into this problem as well. Try this code out (DO NOT MODIFY POSITION OR CODE WILL NOT WORK)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
local function startDrag( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
event.target.bodyType = "kinematic"
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
if ( not event.target.isPlatform ) then
event.target.bodyType = "dynamic"
end
end
end
return true
end
 
function newCrate()
rand = math.random( 100 )
if (rand < 70) then
eggBody = { density=1.0, friction=0.1, bounce=0.2, radius=25 }
object1 = movieclip.newAnim{ "shop1.png", "shop2.png" }
object1.x = _W / 2
object1.y = _H / _H - 50
object1.id = "object1"
physics.addBody( object1, eggBody )
object1.isFixedRotation = true
object1.myName = "object1"
object:addEventListener( "touch", startDrag )
end
end
local dropCrates = timer.performWithDelay( 3000, newCrate, 100 )
<lua>
I haven't tried this out. Let me know how it works. :)

It works!!!
awesome!

Thanks a lot.

views:1503 update:2011/10/5 8:48:05
corona forums © 2003-2011