I am creating an interactive animation of a cat you can stroke, rub, poke etc.
I have implemented some basic functions in an attempt to recognize the different gestures, I am using the mouse down and mouse up events to track the duration and distance of the click.
My problem with this is that the 'distance' is measured from the start point and end point of the click, so if it ends near where it started you won't be able to tell how many times the click has 'rubbed' back and forth.
Is there anyway to track the distance based on the full path of the click? My code is below, does any one have some suggestions?
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, pressed);
this.stage.addEventListener(MouseEvent.MOUSE_UP, released);
var firsttime:Number;
var lasttime:Number;
var firstx:Number;
var lastx:Number;
var firsty:Number;
var lasty:Number;
var duration:Number;
var distance:Number;
function pressed(e:MouseEvent):void {
firsttime = new Date().getTime();
firstx = mouseX;
firsty = mouseY;
trace("mouse down");
}
function released(e:MouseEvent):void {
lasttime = new Date().getTime();
lastx = mouseX;
lasty = mouseY;
duration = lasttime - firsttime;
distance = pythagoras((firstx-lastx), (firsty-lasty));
trace("mouse up");
trace( duration );
trace(distance);
trace(gesture(duration, distance));
}
function pythagoras(xlen:Number, ylen:Number):Number {
return Math.sqrt((xlen*xlen) + (ylen*ylen));
}
function gesture(duration:Number, distance:Number):String {
if (distance < 1 ){
if (开发者_JS百科duration < 200){
return "Sharp Poke";
}
return "Slow Poke";
}
if (duration < 500) {
return "Stroke";
}
return "Rub";
}
From the data you are gathering there is no way of telling what is going on. Basically you have no data other than [start position + time] and [end position + time]
you will need to gather the location of the mouse throughout the gesture.
create a timed event every time the mouse is down (and destroy after mouse up) at an interval of 50ms (just a guess, you'll need to play with this)
every time the timer fires and the mouse is down record the location (and time if the velocity is important)
then on mouse up analyze the data in the array. you can even do a derivative of the data for x and y to see if there was a change in direction.
good luck
here is the link that may help you in solving your problem
http://www.bytearray.org/?p=91
精彩评论