Flashguy’s Blog
Wicked Actionscript

Determining the direction of a Gesture in Flash CS5 on the iPhone

February 5th, 2010 by Gabor Wraight

If you need to find out in what direction a gesture goes, e.g. Up, Down, Left or Right the following Script might help.

Actionscript:
  1. this.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin)
  2. this.addEventListener(TouchEvent.TOUCH_END, onTouchEnd)
  3.  
  4. var startPoint:Point;
  5. var endPoint:Point;
  6.  
  7. function onTouchBegin(e:TouchEvent):void
  8. {
  9.     startPoint = new Point(e.localX, e.localY);
  10. }
  11. function onTouchEnd(e:TouchEvent):void
  12. {
  13.     endPoint = new Point(e.localX, e.localY);
  14.     var a:Number = endPoint.y - startPoint.y;
  15.     var b:Number = endPoint.x - startPoint.x;
  16.    
  17.     if(a*a> b*b){
  18.         if(a> 0){
  19.             //down
  20.         }else{
  21.             //up
  22.         }
  23.     }else{
  24.         if(b> 0){
  25.             //right
  26.         }else{
  27.             //left
  28.         }
  29.     }
  30. }

This entry was posted on Friday, February 5th, 2010 at 12:13 and is filed under Actionscript 3.0, Flash CS5, iphone. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.