Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Using updateAfterEvent with mouse events as an alternative to StartDrag

March 18th, 2010 · 2 Comments · Tutorials

If you have ever used the StartDrag method you have probably noticed that if a user drags an object too quickly its movement becomes erratic and jumpy.  This is because dragging an object initiated by startDrag is synced to the frame rate – normally 30 frames per second.

Unlike startDrag, updateAfterEvent is not synced to the frame rate and is capable of updating much more quickly.  Dragging the slider object above is much more responsive to faster mouse movement with the movement smooth and fluid.

Two additional event listeners which trigger the updateAfterEvent method are added to the stage; the first triggered by the MOUSE_MOVE event and the second by the MOUSE_UP event.  These two listeners attached to the stage allow the user to continue dragging and to initiate stopDrag( ) method when the mouse strays off the object.

import com.dtk.Grid;
 
// basic settings
var bgWidth:uint  = 360;
var bgHeight:uint =  80;
var bgColor:uint  = 0x999999;
 
var trackWidth:uint  = 300;
var trackHeight:uint =   4;
var trackColor:uint  = 0xff0000;
 
var sliderWidth:uint  = 40;
var sliderHeight:uint = 60;
var sliderColor:uint  = 0xffffff;
 
var xOffset:uint;
var resultVal:Number;
 
// draw grid as background
var bg:Grid = new Grid( bgWidth, bgHeight, 10, 0.5, 0xcccccc, 0x999999 );
bg.x = 10;
bg.y =  0; 
addChild( bg );
 
// draw track
var track:Sprite = new Sprite( );
track.graphics.beginFill( trackColor );
track.graphics.drawRect( 30, 70, trackWidth, trackHeight );
track.graphics.endFill( );
bg.addChild( track );
 
// draw slider, set buttonMode to true, add MOUSE_DOWN event listener and name
var slider:Sprite = new Sprite( );
slider.graphics.beginFill( sliderColor );
slider.graphics.drawRoundRect( 20, 10, sliderWidth, sliderHeight, 40 );
slider.graphics.endFill( );
slider.buttonMode = true;
slider.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
slider.name = 'slider';
addChild( slider );
 
/**
 * MOUSE_DOWN event handler
 *
 */
function mouseDownHandler( e:MouseEvent ):void
{
	// define difference between mouse & slider x positions
	xOffset = mouseX - slider.x;
 
	// add event listener to slider for MOVE event & add event listeners for MOVE, UP events to stage
	slider.addEventListener( MouseEvent.MOUSE_MOVE, mouseMoveHandler );
	stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseMoveHandler );
	stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
}
 
/**
 * MOUSE_MOVE event handler
 *
 */
function mouseMoveHandler( e:MouseEvent ):void 
{
	e.updateAfterEvent( );
 
	// calculate volume value (between 0 and 1)
	resultVal = ( trackWidth - slider.x) / trackWidth;
 
	// move slider horizontally with with x offset
	slider.x = mouseX - xOffset;
 
	// maintain y position at initial value
	slider.y = 0;
 
	// limit horizontal movement to left
	if ( slider.x < 0 )
	{
		slider.x = 0;
	}
 
	// limit horizontal movement to right
	if ( slider.x > trackWidth )
	{
		slider.x = trackWidth;
	}
}
 
/**
 * MOUSE_UP event handler
 *
 */
function mouseUpHandler( e:MouseEvent ):void 
{
	// remove active MOUSE_MOVE event listeners
	slider.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}

Download the Grid class used above: Grid.as (188)

Post to Twitter

Tags: ···

2 Comments so far ↓

Leave a Comment

Spam Protection by WP-SpamFree