Detecting mouse gestures in Flash with AS3 – Fixed

This is the reprise of an old post about detecting mouse gestures. Since I am making a quick game for my daughter using mouse (or finger gestures), I took the old algorithm and fixed a bug which prevented to correctly detect the up-right movement. Moreover, I optimized a bit the cde to avoid small movement detection.

This is the resut, draw something in the upper left sector:

The theory is fully explained in the original post, at the moment I am optimizing the code zooming the filtered mouse movement in order to achieve more precision when the user draws tiny lines.

This is the source code:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.text.TextField;
	public class Main extends Sprite {
		private var drawing:Boolean=false;
		private var freeMouse:Sprite=new Sprite();
		private var stepMouse:Sprite=new Sprite();
		private var dirMouse:Sprite=new Sprite();
		private var pX,pY,pX2,pY2:int;
		private var directions:TextField=new TextField();
		private var lastDirection:Number;
		private var currentDirection:Number=-1;
		public function Main():void {
			graphics.lineStyle(4,0x000000);
			graphics.moveTo(320,0);
			graphics.lineTo(320,480);
			graphics.moveTo(0,240);
			graphics.lineTo(640,240);
			addChild(freeMouse);
			addChild(stepMouse);
			addChild(dirMouse);
			addChild(directions);
			stepMouse.x=320;
			dirMouse.y=240;
			directions.x=320;
			directions.y=240;
			directions.height=240;
			addEventListener(Event.ENTER_FRAME,update);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,startDrawing);
			stage.addEventListener(MouseEvent.MOUSE_UP,stopDrawing);
		}
		private function startDrawing(e:MouseEvent):void {
			if (! drawing) {
				directions.text="";
				lastDirection=-1;
				drawing=true;
				freeMouse.graphics.clear();
				freeMouse.graphics.lineStyle(1,0x0000ff);
				freeMouse.graphics.moveTo(mouseX,mouseY);
				stepMouse.graphics.clear();
				stepMouse.graphics.lineStyle(1,0xff0000);
				stepMouse.graphics.moveTo(mouseX,mouseY);
				dirMouse.graphics.clear();
				dirMouse.graphics.lineStyle(1,0x00ff00);
				dirMouse.graphics.moveTo(mouseX,mouseY);
				pX=pX2=mouseX;
				pY=pY2=mouseY;
			}
		}
		private function stopDrawing(e:MouseEvent):void {
			drawing=false;
		}
		private function update(e:Event):void {
			if (drawing) {
				freeMouse.graphics.lineTo(mouseX,mouseY);
				var dX=pX-mouseX;
				var dY=pY-mouseY;
				var distance:Number=dX*dX+dY*dY;
				if (distance>400) {
					stepMouse.graphics.lineTo(mouseX,mouseY);
					var angle:Number=Math.atan2(dY,dX)*57.2957795;
					var refinedAngle:Number;
					var directionString:String;
					if (angle>=22*-1&&angle<23) {
						refinedAngle=0;
						directionString="Left\n";
					}
					if (angle>=23&&angle<68) {
						refinedAngle=Math.PI/4;
						directionString="Up Left\n";
					}
					if (angle>=68&&angle<113) {
						refinedAngle=Math.PI/2;
						directionString="Up\n";
					}
					if (angle>=113&&angle<158) {
						refinedAngle=Math.PI/4*3;
						directionString="Up Right\n";
					}
					if (angle>=158||angle<157*-1) {
						refinedAngle=Math.PI;
						directionString="Right\n";
					}
					if (angle>=157*-1&&angle<112*-1) {
						refinedAngle=- Math.PI/4*3;
						directionString="Down Right\n";
					}
					if (angle>=112*-1&&angle<67*-1) {
						refinedAngle=- Math.PI/2;
						directionString="Down\n";
					}
					if (angle>=67*-1&&angle<22*-1) {
						refinedAngle=- Math.PI/4;
						directionString="Down Left\n";
					}
					pX2-=Math.sqrt(distance)*Math.cos(refinedAngle);
					pY2-=Math.sqrt(distance)*Math.sin(refinedAngle);
					if (refinedAngle!=lastDirection) {
						lastDirection=refinedAngle;
					}
					else {
						if (currentDirection!=lastDirection) {
							directions.appendText(directionString);
							currentDirection=lastDirection;
						}
					}
					dirMouse.graphics.lineTo(pX2,pY2);
					pX=mouseX;
					pY=mouseY;
				}
			}
		}
	}
}

As always you can download the source code. Next time, optimized gesture detection, plus the capability of recognizing simple shapes.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers