DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Actionscript Drag And Pan Acorss A Large Image

03.09.2006
| 10532 views |
  • submit to reddit
        This assumes the registration point is at the top left. 

replace MY_MC with the name of a movieClip on your stage.

import mx.utils.Delegate;
grav = 0.82
img = MY_MC
mouseListener = new Object();
mouseListener.onMouseDown = Delegate.create(this,mouse_down);
mouseListener.onMouseMove = Delegate.create(this,e_point);
mouseListener.onMouseUp   = Delegate.create(this,mouse_up);
Mouse.addListener(mouseListener);


function s_point()
{
	sx_point = _xmouse;
	sy_point = _ymouse;
}

function mouse_down()
{
	pressed = true;
	s_point();
}

function mouse_up()
{
	pressed = false;
}

function e_point()
{
		if(pressed)
		{
			ex_point = _xmouse - sx_point
			ey_point = _ymouse - sy_point
		}
}

function onEnterFrame()
{
	ex_point*=grav;
	ey_point*=grav;
	
	img._x+=ex_point;
	img._y+=ey_point;
	
	if (Math.abs(img._x) > img._width - Stage.width) img._x = (img._width - Stage.width)*-1;
	if (Math.abs(img._y) > img._height - Stage.height) img._y = (img._height - Stage.height)*-1;
	if (img._x > 0) img._x = 0;
	if (img._y > 0) img._y = 0;
}