Create a smooth startDrag() in Flash
Let's face it, startDrag() has been around since wow....flash 4 I think? Perhaps Flash 3, not sure exactly. Sure it serves its purpose still, but one thing that has always bugged me about it ever since updateAfterEvent() became available, is the fact that startDrag is dependant of the frame rate.
I put together a simple MovieClip prototype that would work the same way as startDrag except using updateAfterEvent. It is pretty basic so you could probably add a lot to it, but it does perform the simple task of draggable Objects without being dependant upon the frame rate!
-
MovieClip.prototype.startSmoothDrag = function (lockCenter:Boolean) {
-
if (lockCenter == undefined) var lockCenter:Boolean = false;
-
var thisXMouse:Number = this._xmouse;
-
var thisYMouse:Number = this._ymouse;
-
var setPosition:Function = function (target:Object)
-
{
-
target._x = target._parent._xmouse - ((lockCenter) ? 0 : thisXMouse);
-
target._y = target._parent._ymouse - ((lockCenter) ? 0 : thisYMouse);
-
};
-
setPosition(this);
-
this.onMouseMove = function () {
-
setPosition(this);
-
updateAfterEvent();
-
};
-
};
-
-
MovieClip.prototype.stopSmoothDrag = function () {
-
this.onMouseMove = null;
-
};
-
-
myDraggableMC.onPress = function () {
-
this.startSmoothDrag(true);
-
};
-
-
myDraggableMC.onRelease = myDraggableMC.onReleaseOutside = function () {
-
this.stopSmoothDrag();
-
};
I have also put together a simple demo of this in use, to give you an idea of how it can be effective. Notice below that the fps (frames per second) is set to 1, which is slow if you didn't already know :). Notice how the smooth Dragable obejct is really smooth, while the regular startDrag() method is dependant on that horrifying 1 fps!
Like I said, it is pretty simple but feel free to add to it what you want!


ShinobiMaster said,
June 19, 2008 @ 5:39 pm
Yes! That is great! You need to add a swapDepth() ; to be able to change from the movieClip’s levels.
Or try getNextHighestDepth();
Nick said,
April 25, 2008 @ 4:02 am
Steven,
Excellent work mate, very useful!
-Nick
Jip Spinnewijn said,
March 17, 2008 @ 6:01 am
Thanks!
This was bothering me as well.
Vijay said,
February 4, 2008 @ 5:51 am
Hai,
Yes. Its very useful to mywork. Very Thank to author of this script.
I really appreciate you.
By
Vjay