PDA

View Full Version : [AS2] user mouse movement speed


eddiewireless
09-16-2008, 02:52 AM
I am working on a cross-hair for a shooter game, the client wants the outer bars to expand effecting accuracy depending on how fast the user moves the mouse; so if the user jerks the mouse cursor fast from one end of the screen to the other the cross-hair opens up fast, if he moves it lower it opens up slow...

My problem now is trying to figure out how fast the mouse is moving, what i have now is a function that calculates the difference between the last position of the mouse and the new on, i am running this function using setInterval() every 100ms, then using an if statement to figure out how the speed from the distance the cursor traveled. It is working ok but every now and then it lags...is there another way of doing this?

Indyaner
09-16-2008, 06:43 AM
Mhhh....

How its about a (invisible) Object that goes with the crosshair but in a dull way.

I think about this:
Adding another Object to the screen and let it follow your mouse/crosshair-position (this Tutorial should help (http://www.spoono.com/flash/tutorials/tutorial.php?id=24)). Now you can Calculate everytime the distance between the cursor and the object. From this Value you can calculate the the size of the crosshair what will indicate the "calm down" of the players weapon very well (I guess).

Try it. Report it.

bye

marspark
09-16-2008, 08:00 AM
Maybe you can just try use a tween class, and everytime the mouse moves, it'll recalculate the distance and use a function to determine the speed.

aseptik
11-19-2008, 12:34 PM
this is a good and simple idea how to get the speed of the mouse
http://aendirect.com/articles/design/organic-mouse-controlled-movement-in-actionscript/
instead i wouldn't use a tweening for a mouse-controlled action

eddiewireless
11-19-2008, 10:32 PM
this is what I unded up doing:

class mouseMoveSpeedCheck {
private var l_x:Number;
private var l_y:Number;
private var c_x:Number;
private var c_y:Number;
private var dist:Number;
private var stat:String;
//empty constructor...
function mouseMoveSpeedCheck() {

}
private function Activate():Void {
//last x and y mouse pos
this.l_x = this.c_x;
this.l_y = this.c_y;
//current x and y mouse pos
this.c_x = _xmouse;
this.c_y = _ymouse;
//distance the mouse moved
this.stat = distanceCalc(this.c_x, this.c_y, this.l_x, this.l_y);
trace(this.stat);

}
private function distanceCalc(x1:Number, y1:Number, x2:Number, y2:Number):String {
var dx, dy:Number;
dx = x2-x1;
dy = y2-y1;
this.dist = Math.sqrt(dx*dx+dy*dy);
if (this.dist == 0) {
return "no";
} else if (this.dist<=5 && this.dist>0) {
return "very slow";
} else if (this.dist>5 && this.dist<=20) {
return "slow";
} else if (this.dist>20 && this.dist<=100) {
return "medium";
} else if (this.dist>100) {
return "fast";
}

}
}


usage:

var m:mouseMoveSpeedCheck = new mouseMoveSpeedCheck();


setInterval(m,"Activate",100);


and then i use the returned string to move my crosshair...