Swipe Direction

Posted by Mark Falkland, Posted on October 15, 2011

Here is a way to detect swipe direction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
local beginX 
local beginY  
local endX  
local endY 
 
local xDistance  
local yDistance  
 
function checkSwipeDirection()
 
        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
                        print("swipe left")
                else 
                        print("swipe right")
                end
        else 
                if beginY > endY then
                        print("swipe up")
                else 
                        print("swipe down")
                end
        end
        
end

Object drag direction

Posted by renvis@technowand, Posted on August 2, 2011

The below code shows the direction in which an object is tragged.
The directions shown are
- UP
- UP LEFT
- UP RIGHT
- LEFT
- RIGHT
- DOWN
- DOWN LEFT
- DOWN RIGHT

This is useful in cases where you need to change the image based on the direction of object or if you need to code a joystick.

1
2
3
4
5
6
7
8
9
10
--to store previous position of x and y 
local prevx = 0
local prevy = 0
 
local circle = display.newCircle(250, 250, 50)
-- text to display direction
local directionTXT = display.newText("direction: ",50,50,nil,25) 
 
local function onTouch( event )
        local t = event.target

Simple Analog Stick (Joystick) Module

Posted by x-pressive.com, Posted on May 13, 2011

Here's a clean, simple analog stick (joystick) module for your convenience -use it anywhere and anyhow you like to.

- Create an unlimited amount of analog sticks at once
- Retrieve angle, distance, strength of each stick
- Move your character using a single line only ( MyStick:move() )
- Slim and clean code, easy to customize or extend
- Snaps back smoothly when released (slow-down)
- Working sample included (runs out of the box, no graphics required)

How-to sample code (copy & save as "main.lua")

1
local StickLib   = require("lib_analog_stick")