PDA

View Full Version : Mouse Gestures



grandpaw broon
February 19th, 2009, 07:27 AM
Hi All :D

A new day, a new question!

Not having a right mouse button for my application is pretty annoying and i believe with Macs there is no middle button?

Well i already use the left mouse click and i dont to make the keyboard optional.

Consider the application is a simple line drawing one. It draws a line between each click of the mouse. Well i want to be able to cancel that line drawing and i think what i would like to do is shake the mouse left to right (Almost like black and white when you drop the beasts leash).

I was wondering if anyone knew of any projects using mouse gestures in AS3? Or perhaps a port-able Java one?

If not, does anyone have any ideas on how to check if the mouse has been "shaked" from left to right? Any approach is worth a try.

Cheers,

MurtenSaerbi
February 19th, 2009, 07:59 AM
I once did a project where people had to make circles with the mouse in a game. You could save the mouse position in a timer and then check if the next saved position is smaller or bigger than the current one.

One problem here is that you will need to trial-and-error the speed of movement of the user.

You could incorporate a counter that keeps track of how many times the xPos changes and call a function when you reach "five" or something. That way the user needs to shake a little before the action happens.

If I come up with a better idea I'll let you know.

.ral:cr
February 19th, 2009, 08:01 AM
there is a mouse gestures class that recognises letters, maybe will help.

mac mouses have all 3 buttons and even more, but i'm not shure what you want to do with the middle one, is it recognised in flash?


but, here's my logic for what you want to do. you need an ENTER_FRAME wich will check the current x of the mouse and stores it. the next time you move it you also check the stored value to see in what direction is moving. you keep it stored somewhere the direction. when the direction is changed you increase an index that shows how many times the direction was changed.
now what you have to do is checking into an interval of 1sec lets say, how many times the direction was changed, you predefine some numbers so the shake looks real.

grandpaw broon
February 19th, 2009, 09:09 PM
Thanks for your reply guys.

For some reason .ral:cr i could see what you were saying but it was a headache getting something so simple to work in logic! Basically shaking the mouse left and right so many times in a second triggers the deselection. As my controls are fiddly i didnt want a false positive (deselection by mistake) and a sensitivty of 6 axis-switches per second seems fine.

The solution i have is now part of a larger structure and it wont make sense for me to copy and paste but here is some parts:



private var shakeGestureXCounter:int = 0;
private var shakeGestureXPrevious:int = 0;
private var shakeGestureXDirection:Number = 0; //0 means a negative direction
private var shakeGestureTimer:Timer;


shakeGestureTimer = new Timer(1000);
shakeGestureTimer.addEventListener(TimerEvent.TIME R, onMouseGestureXCheck);
shakeGestureTimer.start();

//UPDATE ONENTERFRAME

if (sprite.stage.mouseX > shakeGestureXPrevious)
{
if (shakeGestureXDirection == 0)
{
shakeGestureXCounter++;
shakeGestureXDirection = 1; //perform the switch
}

}
else if (sprite.stage.mouseX < shakeGestureXPrevious)
{
if (shakeGestureXDirection == 1)
{
shakeGestureXCounter++;
shakeGestureXDirection = 0; //perform the switch
}
}

shakeGestureXPrevious = sprite.stage.mouseX;


private function onMouseGestureXCheck(e:TimerEvent): void
{
trace(shakeGestureXCounter);
if (shakeGestureXCounter > 6)
{
YOUR METHOD CALL CAN GO HERE AS THE GESTURE WORKED
}
shakeGestureXCounter = 0;
}



Sorry about the code format, its been 18 hours and i really must sleep.

Thanks lads.

theMonitor
July 19th, 2011, 09:51 AM
Excellent stuff, grandpaw broon. Massively impressed.