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 )
--************************************************