I have a draggable sprite (a running man), who's animation I want to stop when the user pauses mid-drag. I need some sort of code that recognizes when a draggable object is not moving, but is still in the drag state. I thought of doing something like defining a variable for the sprite's previous position and comparing it to that of it's current position (and checking to see if they are the same), but I'm not sure if this is the best method to use (nor do I know how to write such a code).
If anyone can help me out with this, please let me know.
Here's my code so far, for reference:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | local prevx = 0 local prevy = 0 local dragDirection -- text to display direction local directionTXT = display.newText("direction: ",50,50,nil,25) directionTXT:setTextColor( 255,0,0 ) local function onTouch( event ) local t = event.target 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 ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y prevx = event.x prevy = event.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 local dx = prevx - event.x local dy = prevy - event.y local distance = dx * dx + dy * dy if distance>400 then local angle = math.atan2(dy,dx) * 57.2957795 local string_dir if angle>=45*-1 and angle<=45 then string_dir="Left" if dragDirection ~= "left" then player:play('Player run side view left') dragDirection = "left" end elseif angle>45 and angle<135 then string_dir="Up" if dragDirection ~= "up" then player:play('Player run back view') dragDirection = "up" end elseif angle>135*-1 and angle<45*-1 then string_dir="Down" if dragDirection ~= "down" then player:play('Player run front view') dragDirection = "down" end elseif angle>=135 or angle<=135*-1 then string_dir="Right" if dragDirection ~= "right" then player:play('Player run side view right') dragDirection = "right" end end prevx=event.x prevy=event.y directionTXT.text = "Direction : "..string_dir end elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true end player.x = 300 player.y = 800 player:play('Player static back view') player:addEventListener( "touch", onTouch ) |