How to attach different actions after a drag?

Hello,

I have a few draggable objects in my project. I would like to run a different command (for example, play an animation after user drag object 1, play a sound after user drag object 2, etc) after user drag an object.

Is there a way to attach an “onComplete” kind of function in the event listener? For example:

witch:addEventListener("touch", onDrag, {onComplete = "doIt()"} )  

If not, how could I set the “IFs” inside the drag function below:

 local function onDrag( event )  
 local t = event.target  
  
 print(t)  
  
 local phase = event.phase  
 if "began" == phase then  
 -- Make target the top-most object  
 local parent = t.parent  
 --parent:insert( t )  
 display.getCurrentStage():setFocus( t )  
  
 t.isFocus = true  
   
 -- Store initial position  
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
 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  
 --act\_action\_896()   
 end  
 end  
   
 return true  
 end  

Thanks a lot,
Alex [import]uid: 4883 topic_id: 13049 reply_id: 313049[/import]

In your function you want to add;

[lua]onComplete = print “do stuff here”[/lua]

With “do stuff here” being whatever function you’re calling, or what not :slight_smile:

For example you’d put it on line 25 if you wanted it to trigger when the event “ended” or “cancelled” function was complete.

Peach :slight_smile: [import]uid: 52491 topic_id: 13049 reply_id: 47962[/import]

Peach, where should I add the “onComplete” line? calling the function (if so, how)? Or, in this example, at line 25?

If I should add it at line 25 (which I tried), it triggers to all objects with the drag function attached (which I do not want, as I need different actions to each different object). [import]uid: 4883 topic_id: 13049 reply_id: 47978[/import]

If you want it on each object you could add a listener to the object itself, or say something like

[lua]if event.target == “objectname” then
onComplete = doStuff()[/lua]

What I mean is, onComplete = function() IS the right command to use - where you put it depends on you and what specifically you are doing and HOW you want to do it :slight_smile:

Peach [import]uid: 52491 topic_id: 13049 reply_id: 48051[/import]

Sorry to bother you again (I think I am a slow learner) but it is still not working. Can you please point me where am I doing wrong?

[code]
local function onDrag( event )
local t = event.target
if event.target == “witch” then
onComplete = print(“witch”)
end
if event.target == “monter” then
onComplete = print(“monster”)
end

local phase = event.phase
if “began” == phase then

local parent = t.parent

display.getCurrentStage():setFocus( t )

t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y
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

end
end

return true
end

witch:addEventListener(“touch”, onDrag )
monster:addEventListener(“touch”, onDrag )
[/code] [import]uid: 4883 topic_id: 13049 reply_id: 48057[/import]

Remove the quotes from around “witch” and “monster” in lines 3 and 6 above and it will print :slight_smile: [import]uid: 52491 topic_id: 13049 reply_id: 48170[/import]

You rock Peach! Thanks a lot! [import]uid: 4883 topic_id: 13049 reply_id: 48222[/import]