Select Page

Handy build tool

Here’s a little tool I recently ported to AS3 that I use quite often to place things on the stage at the correct X and Y positions. This saves you from having to guess X and Y values, and it means you can get objects into place quickly and move on to the next thing.
It makes the passed Sprite draggable, and then on release will trace out the new X and Y positions.

package com.sitedaniel.utils {

	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class DesignTool {

		/**
		 * makes a Sprite draggable,
		 * on MOUSE_UP trace the new X & Y values
		 * @param	s: sprite to be dragged
		 */
		public static function dragToPosition(s:Sprite):void {
			s.buttonMode = true;
			s.addEventListener(MouseEvent.MOUSE_DOWN,
					function(e:MouseEvent){
					e.target.startDrag();
					});
			s.addEventListener(MouseEvent.MOUSE_UP,
					function(e:MouseEvent) {
					e.target.stopDrag();
					trace(e.target.x, e.target.y);
					});
		}
	}
}

Usage:

import com.sitedaniel.utils.DesignTool

DesignTool.dragToPosition(spriteToMove);