Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [code snippet] Swipe Left and Right Figure gestures

Author
Message
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 10th Mar 2012 05:03
Hi guys,

Some of you might find this code useful! Its a simple way of getting swipe input from the user.

MikeMax
AGK Academic Backer
12
Years of Service
User Offline
Joined: 13th Dec 2011
Location: Paris
Posted: 10th Mar 2012 07:25
Cool !!! thanks
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 10th Mar 2012 09:51
now we miss swipe up & down
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 10th Mar 2012 13:22
That took nearly twenty seconds to sort out...


Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 10th Mar 2012 13:25
lol
Rich Dersheimer
AGK Developer
14
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 13th Mar 2012 05:54
Very cool
Carolina South
19
Years of Service
User Offline
Joined: 28th Sep 2004
Location: South Carolina
Posted: 14th Mar 2012 04:17
For the inevitable Up Down Left Right needs



"Diplomacy is the art of saying 'Nice doggie' until you can find a rock." - Will Rogers

"The secret to creativity is knowing how to hide your sources." - Albert Einstein
halley
12
Years of Service
User Offline
Joined: 27th Apr 2011
Location: China
Posted: 14th Mar 2012 04:42
I can't understand the code, who can give a explanation?

The Miracrea Games
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 14th Mar 2012 06:48
We begin by assigning some unique identifiers and globalizing them so that they can reach inside functions as required. Time SwipeRightTime starts by grabbing the time passed since the initial pointer was pressed, by doing this we avoid triggering taps, instead of swipes. SwipeRightX stores the X axis value your finger initially fell upon, this is so we can set a minimum "length" of swipe, this prevents the user from simply rocking their finger a little bit and triggering random swipe motion commands. (Not, this tracking information can also be used to scroll objects within that range, for example if you press in the middle of a rolling menu, then range 0 to 100 might simply "scroll" that menu or "move" that sprite, then after 100 it can trigger the "next item in menu" command, this isn't built into these simple commands but it's an easy mod).

Global _UCFSwipeRightTime as Float
Global _UCFSwipeRightX as Integer


Here we just setup the usual display settings. I personally almost always use virtual resolution rather than percentages as I find the process easier to visualise in my head.

SetVirtualResolution( 600, 1024 )
SetDisplayAspect( 600 / 1024 )


Next we create a Do, Sync() and Loop sequence, this allows us to repeat the program cycle to infinity, or when the EXIT command is used to break the loop, in this case I also use the android "Back Key" which is ID 27 to end the program completely when pressed. Remember that END, ends the program, EXIT, just exits the local loop.

The GetSwipeRight( 0.1, 30 ) function specifies the lenth of time the users finger has to be pressed against the glass for it to be considered a swipe motion, this is the 0.1 value (1/10th of a second) and 30 represents the number of virtual pixels your finger needs to move to trigger the final swipe motion. The If statement simply checks to see if the function has returned a 1 state to the program, this lets the program know that all the conditions have been met for the users finger movement to be considered a swipe.

do
If GetSwipeRight( 0.1, 30 ) = 1 then Flag$ = "You Swiped Right"
If GetRawKeyPressed( 27 ) = 1 then END
Print(Flag$)
Sync()
loop


This function is where the magic happens! We start by creating a function name, and passing the values of t#, and s into it, why I selected s as the local identifier I have no idea, it should really be d in retrospect, so just think of s as d, in the same way c stands for the speed of light in mathematics. It doesn't make sense, it's just one of those things, (actually c means celeritas, which is Latin for speed, and c represents the speed of light within a vacuum, worth noting since the speed of light varies through different materials, for example scientists fired a laser beam through super cold sodium atoms reducing it down to just 38 mph). Anyway back to the program! s means distance, and that's that. Feel free to change it. Once we have those values, (t#, is a float with the value of 0.1, 1/10th of a second remember? s is an integer with the value of 30 pixels).

Function GetSwipeRight( t#, s )

We start our function code with Output = 0, this isn't really needed because local variable's within a function should be reset to 0 when the function starts anyway, but I do this really so I can follow the programs logic more easily. Output is the final value we will output from the function, if the function returns 0 then the if statement will not trigger, if it reaches 1 then it will.

Output = 0

Here we get the pointer state of the program, by "pointer" we mean your finger. If the pointer state is 0 then we set the Time global to the current timer the value of time we need it increased by. This means the Time value never matches the timer itself. Really this is a little wasteful, as it's constantly grabbing timer() the logic works but could be improved. For example GetPointerPressed doesn't return a constant 1 when held down, it litarily gets the point at which it is pressed. By removing the first IF and simply adding it's then statement into the GetPointerPressed() if sequence, it wouldn't need to constantly update the Time global. But I'm explaining the function as it is, flawed logic and all. Logically my method works but as I say it's a little bit wasteful. Anyway, the first IF statement simply checks to see if the user is pressing the screen, if it isn't we update the time global and add the time value to it. The second If statement checks to see if the screen was pressed, when it is we store the X value at the point of the press. Remember that X represents the X axis on your screen, we grab this information with GetPointerX() then we add the minimum required distence for the finger to move with s.

If GetPointerState() = 0 then _UCFSwipeRightTime = Timer() + t#
If GetPointerPressed() = 1 then _UCFSwipeRightX = GetPointerX() + s


This is where the magic happens! Again, a lot of magic in this little program. But we have the data we need, time to check it, the first If statement checks to see if the pointer state is 1, if it is this means our finger is pressed against the glass (or possibly the cheap plastic) but that isn't enough, we also need to ensure that the stored time value is less than the current Timer() this means that the time value we stored is old enough for the timer() itself to have moved on. To be honest I don't really need another IF statement after that, I could shove another "and _UCFSwipeRightX < GetPointerX()" into the first if, this basically checks to see if your finger has moved at least 30 pixels to the right, compared to that initial first press, if it has, then we've satisifed the minimum required "movement" of the finger across the glass. So we're checking 3 things, is the finger on the screen, has the minimum required time elapsed to consider this a swipe of any kind and has the minimum required distance elapsed. If all three conditions are met we trigger we set the Output to 1 and reset the current time value to zero.

If GetPointerState() = 1 and _UCFSwipeRightTime < Timer()
If _UCFSwipeRightX < GetPointerX()
_UCFSwipeRightTime = 0
Output = 1
EndIf
EndIf


Finally we ensure that the Output value is passed from our function and into the program.

EndFunction Output

Writing this has given me some idea to tidy up this function, as a proof of concept this works just fine, but I can streamline the code a little. Pop back soon for the updated version!
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 14th Mar 2012 06:57
Updated version! Reduced code, and a little less wasteful, this should also make the code easier to read and understand.

Login to post a reply

Server time is: 2024-04-20 03:46:10
Your offset time is: 2024-04-20 03:46:10