Codepaste
Paste some code
Upload a file...
Open »
You have no editable files. Paste some code, or add a public key and you'll see the files you can edit listed here.
Add a public key...
File
Save as a new paste
Properties...
Email to a friend...
Download the code...
Help
The Codepaste forum
About...
test
package com.gigglingcorpse.utils; import nme.display.DisplayObject; import nme.display.Stage; import nme.events.Event; import nme.events.MouseEvent; import nme.geom.Point; import nme.Lib; /** * ... * @author brad */ class DragVelocity { // Me! private static var _instance:DragVelocity; // Timing private static var _lastTime:Float; private static var _delta:Float; private static var _now:Float; // Velocity tracking private static var _lastPosition:Point; private static var _velocity:Point; public static var velocity( getVelocity, null ):Point; // Convenience data private static var _surface:DisplayObject; private static var _started:Bool; /** * You can also start by calling new DragVelocity(). That's cool too. But kind of weird. */ public function new( ) { if ( _instance == null ) { _velocity = new Point( 0, 0 ); _surface = Lib.current.stage; _delta = 0; _instance = this; _started = false; start(); } } /** * Start the DragVelocity util. DragVelocity.start(); can (and should) be your first call. */ public static function start() { if ( _instance == null ) new DragVelocity(); if ( ! _started ) { _lastTime = Lib.getTimer(); _surface.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown ); _surface.addEventListener( MouseEvent.MOUSE_UP, onMouseUp ); _surface.addEventListener( Event.ENTER_FRAME, onFrame ); _started = true; } } /** * Stop the util! Any further requests for DragVelocity.velocity will result in (0,0). * To start it up again, call DragVelocity.start(). */ public static function stop() { if ( _instance == null ) return; if ( _started ) { _surface.removeEventListener( MouseEvent.MOUSE_DOWN, onMouseDown ); _surface.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp ); _surface.removeEventListener( Event.ENTER_FRAME, onFrame ); _velocity.x = 0; _velocity.y = 0; _started = false; } } /** * Returns the current velocity. I wonder if i should make it .clone()... * @return the current drag velocity. */ private static function getVelocity() : Point { return _velocity; } private static inline function onMouseDown( e:MouseEvent ) { _lastPosition = new Point( e.stageX, e.stageY ); } private static inline function onMouseUp( e:MouseEvent ) { _lastPosition = null; } /** * Figures out the current velocity. * @param e */ private static inline function onFrame( e:Event ) { _now = Lib.getTimer(); _delta = _now - _lastTime; // update velocity if ( _lastPosition != null ) { var p = new Point( Lib.current.stage.mouseX, Lib.current.stage.mouseY ); var distance = p.subtract( _lastPosition ); _velocity.x = distance.x / _delta; _velocity.y = distance.y / _delta; _lastPosition = p; } else { _velocity.x = 0; _velocity.y = 0; } _lastTime = _now; } }
hide
Web development by Altered Effect