Help Needed - More 'Natural' Drag to Move controls

Hi guys,

I’m currently working on top down plane shooter. Unfortunately, I can’t seem to get the controls to work right. Below are my codes, modified from a Drag and Move tutorial (can’t remember where I took them from) 

function touchScreen(event) if (event.phase == "began") then mainCharacter.isFocus = true mainCharacter.x0 = event.x - mainCharacter.x mainCharacter.y0 = event.y - mainCharacter.y elseif mainCharacter.isFocus then local speed = 10 -- just putting it here temporary if "moved" == event.phase then -- mainCharacter.x = event.x -- mainCharacter.y = event.y mainCharacter:setLinearVelocity((event.x - mainCharacter.x) \* speed, (event.y - mainCharacter.y) \* speed) elseif "ended" == phase or "cancelled" == phase then mainCharacter.isFocus = false mainCharacter:setLinearVelocity(0, 0) end end return true end

The controls i’m looking for is something similar to the game Sky Force, where the plane will move to the position of the player’s finger base on its own speed, which makes it feel more natural when you move your fingers along the screen. 

The problem with my code is, the plane will continue to move despite not moving my finger. Same goes when my finger is lifted from the screen. I tried using transition.to but it makes the plane ‘hiccup’ during movement.

Any help is appreciated  :slight_smile:

You need to move your mainCharacter move code into a frame event then in your touch set the currentX and currentY etc. 

@Christopher Bishop

Thank you so much! That did the trick.  :slight_smile:

For those interested in implementing similar controls, here’s are the codes I’m currently using.

function moveCharacter() local speed = 5 if (mainCharacter.targetX ~= nil) then mainCharacter:setLinearVelocity((mainCharacter.targetX - mainCharacter.x) \* speed, (mainCharacter.targetY - mainCharacter.y) \* speed) end end function touchControl(event) if (event.phase == "began" or event.phase == "moved") then mainCharacter.targetX = event.x mainCharacter.targetY = event.y elseif (event.phase == "ended" or event.phase == "cancelled") then -- other codes end return true end Runtime:addEventListener("enterFrame", moveCharacter) Runtime:addEventListener("touch", touchControl)  

You need to move your mainCharacter move code into a frame event then in your touch set the currentX and currentY etc. 

@Christopher Bishop

Thank you so much! That did the trick.  :slight_smile:

For those interested in implementing similar controls, here’s are the codes I’m currently using.

function moveCharacter() local speed = 5 if (mainCharacter.targetX ~= nil) then mainCharacter:setLinearVelocity((mainCharacter.targetX - mainCharacter.x) \* speed, (mainCharacter.targetY - mainCharacter.y) \* speed) end end function touchControl(event) if (event.phase == "began" or event.phase == "moved") then mainCharacter.targetX = event.x mainCharacter.targetY = event.y elseif (event.phase == "ended" or event.phase == "cancelled") then -- other codes end return true end Runtime:addEventListener("enterFrame", moveCharacter) Runtime:addEventListener("touch", touchControl)