Dragging and Dropping in Actionscript 3.0

Ok this is a little tip for Dragging your clips on Actionscript 3.0, remember how we used a lot of tricks in Flash to drag a clip from other points that weren’t the anchor point , well in Actionscript 3.0 (herenow AS3) is quite simple to do that by using the new Point class.
Class Point.
Package flash.geom
Class public class Point
InheritancePoint Object
Language version: ActionScript 3.0
Player version: Flash Player 9
The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.

So here’s a very short example made in AS3 in wish no matter where your grab the Sprite, it will be dragged from that specific Point.
Heres the example :

This movie requires Flash Player 10

Open in New Window

Here’s the code:

?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bear.addEventListener(MouseEvent.MOUSE_DOWN, startbearDrag);
stage.addEventListener(MouseEvent.MOUSE_UP, stopbearDrag);
bear.addEventListener(Event.ENTER_FRAME, dragbear);
 
var clickOffset:Point = null;
 
function startbearDrag(event:MouseEvent) {
	clickOffset = new Point(event.localX, event.localY);
}
 
function stopbearDrag(event:MouseEvent) {
	clickOffset = null;
}
 
function dragbear(event:Event) {
	if (clickOffset != null) {
		bear.x = mouseX - clickOffset.x;
		bear.y = mouseY - clickOffset.y;
	}
}

Author: Dave Gamez

Digital Craftsman from México and have more than 14 years of experience both in design and development in the digital field. I currently work in web, desktop, mobile and illustration projects alike and spend my time between being Technology Director at Team-O a Digital Studio I co-founded with Cecy Meade and doing consultancy work, when time allows it I like to experiment and tinker with code, animation and illustration.