In my game right now, im controlling the gravity just by tapping. i want to change the controls so that when i swipe up the physics.gravity is -physics.gravity() and when i swipe down its regular gravity. i will post my code if anyone asks for more help. i just can’t get the touch.deltaY down. any help? i tried referrring to @ruilov’s pacman example but couldn’t find that code snippet
What determines what constitutes a ‘vertical swipe’? Is it where the touch ends compared to where it started? For example:
function setup()
end
function draw()
background(0)
end
function touched(touch)
local id = touch.id
local state = touch.state
local x = touch.x
local y = touch.y
if state == BEGAN then
swipeId = id -- Preserve the id
swipeX = x -- Preserve the position
swipeY = y
return
end
if state == ENDED and id == swipeId then
swipeId = nil
if math.abs(x - swipeX) < 100 then -- Within tolerance?
if swipeY - y > 100 then -- Long enough?
print("Swipe down")
end
if y - swipeY > 100 then -- Long enough?
print("Swipe up")
end
end
end
end
thanks @mpilgrem. just a quick question is there any way to detect with a user swipes up twice. For example, right now when you swipe up or down in my game, the gravity changes and you lose a point. Is there any way to detect if a person swiped up twice and ignore the function to print swipe up twice. it’s just a little bug in my game and I’m not sure how to fix it.
You’ll need to track whether the user has already made a swipe and need to allow them a certain amount of time to get a second one in. The following adapts @mpilgrem’s example for the up swipe only. If you add more interactions the logic will become more complex and you may want to spend more time thinking out a more elegant solution (e.g. What happens with an up and down swipe. Or an up swipe and a short tap), but this rough solution should work. Note the locations of the print statements this represents where the decision on a single or double swipe is met.
function setup()
firstswipe=0
timer=0
timeout=20
end
function draw()
background(0)
if firstswipe==1 then
timer = timer + 1
if timer>timeout then --time limit for user to get a second swipe in
timer=0
firstswipe=0
print("Swipe up")
end
end
end
function touched(touch)
local id = touch.id
local state = touch.state
local x = touch.x
local y = touch.y
if state == BEGAN then
swipeId = id -- Preserve the id
swipeX = x -- Preserve the position
swipeY = y
return
end
if state == ENDED and id == swipeId then
swipeId = nil
if math.abs(x - swipeX) < 100 then -- Within tolerance?
if swipeY - y > 100 then -- Long enough?
print("Swipe down")
end
if y - swipeY > 100 and firstswipe==0 then -- Long enough?
firstswipe=1
elseif y - swipeY > 100 and firstswipe==1 then -- Long enough?
print("Double Swipe up")
firstswipe=0
end
end
end
end
I detected horizontal and vertical swipes in my program (I don’t have access to the code now, but I might post it later) by checking if the length of the swipe is greater than a certain number, then checking which direction it is closest to. This makes the swipe length the same, even if the user swipes at a slight angle. I found this more comfortable than checking just the x or y length, which would require the user to swipe longer if they are swiping at an angle, providing less consistency.