Rotate platform function?

I have a rotate/drag platform function that is working (it’s pretty confusing). It works like, the first tap drags the platform across the screen and then the second tap rotates it, then third tap drags it etc… I would like to make it that one tap and hold drags the platform and a double tap and hold rotates it, but I can’t seem to figure this out.

Here’s the code

--------------------------------------------------------------------------- Rotate platform local function movePlatform(event) platformTouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( platformTouched ) if platformTouched.touchMode == "move" then -- here the first position is stored in x and y platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y platformTouched.x1 = event.x platformTouched.y1 = event.y elseif platformTouched.touchMode == "rotate" then end elseif (event.phase == "moved") then if platformTouched.touchMode == "move" then -- here the distance is calculated between the start of the movement and its current position of the drag platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY elseif platformTouched.touchMode == "rotate" then platformTouched.x2 = event.x platformTouched.y2 = event.y angle1 = 180/math.pi \* math.atan2(platformTouched.y1 - platformTouched.y , platformTouched.x1 - platformTouched.x) angle2= 180/math.pi \* math.atan2(platformTouched.y2 - platformTouched.y , platformTouched.x2 - platformTouched.x) differencebetweenangles = angle1 - angle2 --rotate it platformTouched.rotation = platformTouched.rotation - differencebetweenangles platformTouched.x1 = platformTouched.x2 platformTouched.y1 = platformTouched.y2 end elseif event.phase == "ended" or event.phase == "cancelled" then --each time we release the platform, toggle between move and rotate mode if platformTouched.touchMode == "move" then platformTouched.touchMode = "rotate" else platformTouched.touchMode = "move" end display.getCurrentStage():setFocus( nil ) end return true end platform:addEventListener("touch", movePlatform)

Perhaps I’m being picky this morning, but please format your code to be more readable when posting.

Your current code is trying to switch the type of motion based on the end phase, which won’t be counting taps, because this is the touch function. You would need to count the taps yourself.

I would do it with a tap and a touch listener - though this is one of the very rare situations where that is the case.

I will also assume that you mean a simple touch and drag performs a drag and a tap followed by a drag performs a rotate.

local function angleOnTarget( e ) local a = math.atan2( e.y-e.target.y, e.x-e.target.x ) \* 180 / (4\*math.atan(1)) if (a\<0) then a=a+360 end return a end local function tap(e) e.target.touchMode = "drag" print("drag mode") e.target.dragtimer = timer.performWithDelay( 400, function() e.target.touchMode = "move" print("move mode") end ) return true end local function touch(e) if (e.phase == "began") then e.target.hasFocus = true display.currentStage:setFocus( e.target ) e.target.touchMode = e.target.touchMode or "move" e.target.prev = e if (e.target.dragtimer) then timer.cancel( e.target.dragtimer ) e.target.dragtimer = nil end e.target.prevangle = angleOnTarget( e ) return true elseif (e.target.hasFocus) then if (e.phase == "moved") then if (e.target.touchMode == "move") then e.target.x, e.target.y = e.target.x+(e.x-e.target.prev.x), e.target.y+(e.y-e.target.prev.y) else local angle = angleOnTarget( e ) e.target.rotation = e.target.rotation + (angle - e.target.prevangle) e.target.prevangle = angle end e.target.prev = e else e.target.hasFocus = nil e.target.prev = nil display.currentStage:setFocus( nil ) e.target.touchMode = "move" e.target.prevangle = nil end return true end return false end local rect = display.newRect( display.contentCenterX, display.contentCenterY, 300, 150 ) rect:addEventListener( "touch", touch ) rect:addEventListener( "tap", tap )

Btw, for this to work properly in the simulator, please remember that if you’re on a Mac with a touch pad, you will need to fully press on the track pad for the tap to register and switch to rotate mode.

Perhaps I’m being picky this morning, but please format your code to be more readable when posting.

Your current code is trying to switch the type of motion based on the end phase, which won’t be counting taps, because this is the touch function. You would need to count the taps yourself.

I would do it with a tap and a touch listener - though this is one of the very rare situations where that is the case.

I will also assume that you mean a simple touch and drag performs a drag and a tap followed by a drag performs a rotate.

local function angleOnTarget( e ) local a = math.atan2( e.y-e.target.y, e.x-e.target.x ) \* 180 / (4\*math.atan(1)) if (a\<0) then a=a+360 end return a end local function tap(e) e.target.touchMode = "drag" print("drag mode") e.target.dragtimer = timer.performWithDelay( 400, function() e.target.touchMode = "move" print("move mode") end ) return true end local function touch(e) if (e.phase == "began") then e.target.hasFocus = true display.currentStage:setFocus( e.target ) e.target.touchMode = e.target.touchMode or "move" e.target.prev = e if (e.target.dragtimer) then timer.cancel( e.target.dragtimer ) e.target.dragtimer = nil end e.target.prevangle = angleOnTarget( e ) return true elseif (e.target.hasFocus) then if (e.phase == "moved") then if (e.target.touchMode == "move") then e.target.x, e.target.y = e.target.x+(e.x-e.target.prev.x), e.target.y+(e.y-e.target.prev.y) else local angle = angleOnTarget( e ) e.target.rotation = e.target.rotation + (angle - e.target.prevangle) e.target.prevangle = angle end e.target.prev = e else e.target.hasFocus = nil e.target.prev = nil display.currentStage:setFocus( nil ) e.target.touchMode = "move" e.target.prevangle = nil end return true end return false end local rect = display.newRect( display.contentCenterX, display.contentCenterY, 300, 150 ) rect:addEventListener( "touch", touch ) rect:addEventListener( "tap", tap )

Btw, for this to work properly in the simulator, please remember that if you’re on a Mac with a touch pad, you will need to fully press on the track pad for the tap to register and switch to rotate mode.