var mouse_obj = { 
        nx:null, ny:null, ox:null, oy:null,
        velx:0, vely:0, mov:0, target:_root,
        init: function( $target )
        {
                with ( this )
                {
                        target = $target;
                        nx = ox = target._xmouse;
                        ny = oy = target._ymouse;
                        velx = vely = mov = 0;
                }
        },
        update: function()
        {
                with ( this ) 
                {
                        ox = nx
                        oy = ny
                        nx = target._xmouse;
                        ny = target._ymouse;
                        velx = nx - ox;
                        vely = ny - oy;
                        mov = Math.sqrt((velx*velx) + (vely*vely));
                }
        }
};

onMouseDown = function()
{
        mouse_obj.init( _root );
        this.onEnterFrame = function()
        {
                mouse_obj.update();
                
                trace ( 'velx: ' + mouse_obj.velx )
                trace ( 'vely: ' + mouse_obj.vely )
                trace ( 'mov: ' + mouse_obj.mov + newline )
        };
}

onMouseUp = function()
{
        this.onEnterFrame = null;
}

_____________________________

Jesse Graupmann
www.jessegraupmann.com  
www.justgooddesign.com/blog/  
_____________________________



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of eric e.
dolecki
Sent: Wednesday, June 20, 2007 10:29 AM
To: Flashcoders mailing list
Subject: [Flashcoders] Mouse Velocity?

I am calculating mouse velocity, but every now and then you get a 0 for
velocity eventhough its not 0 (setInterval prob I suspect).  Any better
approach?

var MouseX;
var MouseY;

function determineVelocity():Void
{
    MouseX = _root._xmouse
    MouseY = _root._ymouse
    setTimeout( calc, 9 );
};

setInterval( determineVelocity, 20 );

function calc():Void
{
    var newMouseX:Number = _root._xmouse;
    var newMouseY:Number = _root._ymouse;

    var deltaX = Math.abs(MouseX - newMouseX)
    deltaY = Math.abs(MouseY - newMouseY)
    dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY))
    velocity = dist*31; // 31 = fps
    trace( velocity );
};


_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to