Sorry, Here is the simplified code for the touch event:
-- object to be used local \_obj = display.newRect(display.contentWidth \* 0.5, display.contentHeight \* 0.5, display.contentWidth \* 0.9, display.contentWidth \* 0.9) -- Parameters local throwLeftMargin = display.contentWidth \* 0.3 local throwRightMargin = display.contentWidth \* 0.7 local throwRotationMargin = 10 local throwSpeed = 500 local throwRotation = 180 local putBackSpeed = 500 -- transition in milliseconds -- Get the object position local objStartX = \_obj.x local objStartY = \_obj.y -- Prepare variables local startX = 0 -- starting position of the touch event local startY = 0 local noTouch = false local function touchListener(event) if noTouch then -- return if any transition is showing return false end local phase = event.phase local eventX = event.x local eventY = event.y local obj = event.target if phase == "began" then -- get the starting position of the touch event startX = eventX startY = eventY -- do any additional things here for event start if needed elseif phase == "moved" then obj.x = objStartX + (eventX - startX) obj.y = objStartY + (eventY - startY) obj.rotation = 45 \* ((event.x - startX) / display.contentWidth) -- do any additional things here for the moving event if needed elseif phase == "cancelled" or phase == "ended" then local function onTransitionStart(event) noTouch = true end if obj.rotation ~= 0 then -- if rotation == 0, it means no movement has been made if eventX \> throwRightMargin and obj.rotation \> throwRotationMargin then -- Throw the object to the right local function transitionDone(event) print('Throw Right Done') -- fire event or do something here noTouch = false end transition.to( obj, { time=throwSpeed, x=(display.contentWidth \* 2), y=eventY, rotation=throwRotation, onStart=onTransitionStart, onComplete=transitionDone} ) elseif eventX \< throwLeftMargin and obj.rotation \< -throwRotationMargin then -- Throw the object to the left local function transitionDone(event) print('Throw Left Done') -- fire event or do something here noTouch = false end transition.to( obj, { time=throwSpeed, x=-display.contentWidth, y=eventY, rotation=-throwRotation, onStart=onTransitionStart, onComplete=transitionDone} ) else -- put back the object to the original place local function transitionDone(event) print('Not Throw') -- fire event or do something here noTouch = false end transition.to( obj, { time=putBackSpeed, x=objStartX, y=objStartY, rotation=0, onStart=onTransitionStart, onComplete=transitionDone} ) end end end end \_obj:addEventListener("touch", touchListener)
I replaced _obj to group object in the real app. And added other display object into the group.