I would like to never swipe in a direction twice

Hi !

I am developping a game where you can use swipe gesture in a direction to make move a box.

I am using this code to detect the swipe:

local beginX   
local beginY   
local endX   
local endY   
   
local xDistance   
local yDistance  
   
local bDoingTouch  
local minSwipeDistance = 50  
local totalSwipeDistanceLeft  
local totalSwipeDistanceRight  
local totalSwipeDistanceUp  
local totalSwipeDistanceDown  
   
function checkSwipeDirection()  
 if bDoingTouch == true then  
 xDistance = math.abs(endX - beginX) -- math.abs will return the absolute, or non-negative value, of a given value.   
 yDistance = math.abs(endY - beginY)  
 if xDistance \> yDistance then  
 if beginX \> endX then  
 totalSwipeDistanceLeft = beginX - endX  
 if totalSwipeDistanceLeft \> minSwipeDistance then  
 print("Swiped Left")  
 end  
 else   
 totalSwipeDistanceRight = endX - beginX  
 if totalSwipeDistanceRight \> minSwipeDistance then  
 print("Swiped Right")  
 end  
 end  
 else   
 if beginY \> endY then  
 totalSwipeDistanceUp = beginY - endY  
 if totalSwipeDistanceUp \> minSwipeDistance then  
 print("Swiped Up")  
 end  
 else   
 totalSwipeDistanceDown = endY - beginY  
 if totalSwipeDistanceDown \> minSwipeDistance then  
 print("Swiped Down")  
 end  
 end  
 end  
 end  
 end  
 function swipe(event)  
 if event.phase == "began" then  
 bDoingTouch = true  
 beginX = event.x  
 beginY = event.y  
 end  
 if event.phase == "ended" then  
 endX = event.x  
 endY = event.y  
 checkSwipeDirection();  
 bDoingTouch = false  
 end  
end  
   
Runtime:addEventListener("touch", swipe)  

When I swipe in a direction, say, to the north, the box is moving to the north. Great ! Now, say the box hit a wall after moving north and stop against it by dectecting the collision. By the way, my box move by applying box:applyForce(0, 5000, box.x, box.y). So, if I want to swipe to the north again, an unexpected behavior happens. Indeed, the force applyed will be doubled by rebounding against the wall and the box will move twice faster than expected…in the reverse direction ! This is not the right behavior that I was expected.

Basically, I don’t want the box to rebound against the wall when I swipe twice in that particular direction.
If I move a box against the wall by swipping to the north, then if I swipe north again, I would like to ignore that swipe as long as the user is trying to swipe north and that the box is colliding with the wall, so the force won’t be applyed and the box won’t rebound. So the only swipe gesture available to the player would be south, east and west.

Could someone help me with that problem ?

Thank you in advance ^^

Ray [import]uid: 20617 topic_id: 33001 reply_id: 333001[/import]

Set a flag for when the box collides with the wall. eg, boxAtWall = true - set to true on collision, false at ended phase, only allow swipe/apply force if boxAtWall == false. [import]uid: 52491 topic_id: 33001 reply_id: 131002[/import]

Set a flag for when the box collides with the wall. eg, boxAtWall = true - set to true on collision, false at ended phase, only allow swipe/apply force if boxAtWall == false. [import]uid: 52491 topic_id: 33001 reply_id: 131002[/import]