Hi all,
I was wondering if anybody had any ideas as the best way to detect swipe in four directions.
Here’s my thinking:
I want to make it so that when on the screen the user swipes left/right/up, different actions are run. However, I would like it to detect the swipe without the user taking their finger off the screen.
For example, the player swipes left and holds their finger in a position to the left of their initial swipe, then the function will continue to execute. Once the finger is released from the screen, the triggered function will stop. The same thing will happen in the other four directions.
Another thing I want it to be able to do is detect swipes from current relative positions.
For example:
1 2 3
If the user starts their tap near the number two, and swipes towards one, without releasing their finger, then a specific function will be executed. Then, if the person decides to swipe back to the right towards 2/3, another function will be executed (without the need to go past the initial start point, but rather from its previous position.
Here is what I currently have (from an example I found on the documentation):
local function handleSwipe( event ) if ( event.phase == "moved" ) then local dX = event.x - event.xStart local dY = event.y - event.yStart if ( dX \> 5 ) then --swipe right local spot = XRIGHT if ( event.target.x == XLEFT ) then spot = centerX end transition.to( player, { time=500, x=spot } ) elseif ( dX \< -5 ) then --swipe left local spot = XLEFT if ( event.target.x == XRIGHT ) then spot = centerX end transition.to( player, { time=500, x=spot } ) elseif ( dY \< -5 ) then if contact == 1 then --swipe up local spot = YLEFT if ( event.target.y == YRIGHT ) then spot = centerY end transition.to( player, { time=100, y=player.y - 10 } ) end end end return true end local button = display.newRect( 0, 0, 0, 0 ) button:setFillColor( 250/255, 250/255, 250/255 ) button.height = \_Y \* 2; button.width = \_X \* 2 button.x = 0; button.y = 0 button.anchorX = 0 button.isVisible = false button.isHitTestable = true button:addEventListener( "touch", handleSwipe )
Basically, when I swipe left the player moves all the way to the left. I would rather have the player gradually move to the left (not all the way on the initial swipe), and the same for the right. That way the user continues their established swipe to have the player continue movement until it hits the left/right borders.
I know this is very confusing, but any help is greatly appreciated.
Thanks