Tag Archives: velocity

Mouse Velocity

Actionscript:
  1. var velocityInfo:TextField = new TextField();
  2. velocityInfo.x = 20;
  3. velocityInfo.y = 20;
  4. addChild(velocityInfo);
  5.  
  6. var prevX:Number = mouseX;
  7. var prevY:Number = mouseY;
  8. var velX:Number = 0;
  9. var velY:Number = 0;
  10.  
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12.  
  13. function onLoop(evt:Event):void {
  14.      
  15.      velX = mouseX - prevX;
  16.      velY = mouseY - prevY;
  17.      
  18.      velocityInfo.text = velX + ", " + velY
  19.      
  20.      prevX = mouseX;
  21.      prevY = mouseY;
  22. }

Wrote this in response to a question from one of my students. The next step is to use velX and velY to push objects around the screen.

Posted in motion | Also tagged , , | 5 Comments