Friday, May 15, 2009

Tutorial: Handling touch gestures in Flash Lite

I came across this interesting tutorial on how to handle touch gestures made famous by Iphone in a Flash Lite application.

Here's the code from the tutorial:

var startX:Number;
var startY:Number;
var endX:Number;
var endY:Number;

var gesturesListener:Object = new Object();

gesturesListener.onMouseDown = function()
{
  startX = _root._xmouse;
  startY = _root._ymouse;
}

gesturesListener.onMouseUp = function()
{
  endX = _root._xmouse;
  endY = _root._ymouse;

  checkGesture();
}

Mouse.addListener(gesturesListener);

// minimum length of an horizontal gesture
var MIN_H_GESTURE:Number = Stage.width / 3;
// minimum length of a vertical gesture
var MIN_V_GESTURE:Number = Stage.height / 3;

// flags for each kind of gesture
var UP_TO_DOWN:Number = 1;
var DOWN_TO_UP:Number = 2;
var LEFT_TO_RIGHT:Number = 4;
var RIGHT_TO_LEFT:Number = 8;

function checkGesture()
{
  var xDelta:Number = endX - startX;
  var yDelta:Number = endY - startY;

  var gesture:Number = 0;

  if(xDelta > MIN_H_GESTURE)
    gesture |= LEFT_TO_RIGHT;
  else if(xDelta < - MIN_H_GESTURE)
    gesture |= RIGHT_TO_LEFT;
  if(yDelta > MIN_V_GESTURE)
    gesture |= UP_TO_DOWN;
  else if(yDelta < - MIN_V_GESTURE)\
    gesture |= DOWN_TO_UP;
  if(gesture > 0)
    handleGesture(gesture);
}

function handleGesture(gestureFlags:Number)
{
  if(gestureFlags & LEFT_TO_RIGHT)
    trace("left to right gesture");
  if(gestureFlags & RIGHT_TO_LEFT)
    trace("right to left gesture");
  if(gestureFlags & UP_TO_DOWN)
    trace("up to down gesture");
  if(gestureFlags & DOWN_TO_UP)
    trace("down to up gesture");
}


It was from a mobile games development blog, Jappit which covers all the different platforms, namely J2Me, Flash Lite, Symbian, Iphone and Andriod.

No comments:

Post a Comment