Snap to

how can I make only selected draggable movie clips “snap to” a non-draggable movie clip? Can anyone help me out? thx.

:m:

I can help you with the snap in, but… what do you mean “only selected draggable movie clips”?

i have a bunch of draggable movie clips, but i only want a few to be able to snap in. But, I think it would be allright if all the movieclips could snap in. understand? thx.

do you know how to do hit test?

If you do, you basically want to say "if the movie I am dragging is sitting somewhere near the target movie, make the registration point (movie._x) equal to the target registration point.

Does that make sense?

make a function to get the distance between the movieclip you are dragging and the target clips… then once its small enuff snap them together…


MovieClip.prototype.snap = function (x, y)
{
	w = this._x;
	z = this._y;
	xd = (x - w);
	yd = (y - z);
	hyp = Math.sqrt (xd * xd + yd * yd);
	return hyp;
};
mm.onPress = function ()
{
	this.startDrag (true);
	this.onEnterFrame = function ()
	{
		if (this.snap (_root.cc._x, _root.cc._y) <= 150)
		{
			this.stopDrag ();
			this._x = _root.cc._x;
			this._y = _root.cc._y;
		}
	};
};

thats what I came up with. I’m sure there is a better way tho…

mm is the name of the clip you are dragging and cc is the name of the target clip. 150 is the distance between them.

bumping up

anybody want to try to make my code better?

sorry. either im putting in the code wrong or the code dont work. on what should i put the code on? the target, the clip im draggin, or both? thanks

put that code in the main timeline, and name the draggable movieclip “mm” and the target movieclip “cc”

[AS]MovieClip.prototype.snap = function(x, y) {
w = this._x;
z = this._y;
xd = (x-w);
yd = (y-z);
hyp = Math.sqrt(xdxd+ydyd);
return hyp;
};
mm.onPress = function() {
this.startDrag(true);
};
mm.onRelease = function() {
if (this.snap(_root.cc._x, _root.cc._y)<=150) {
this.stopDrag();
this._x = _root.cc._x;
this._y = _root.cc._y;
} else {
stopDrag();
}
};[/AS]

That gives a snap to ONLY if when you release the clip it is within 150px from the other clip.

so… i got lostinbeta’s code to work but i cant drag the clip anywhere else without the clip snapping back to the target. how can i change it so there is only a specific radius where the clip will snap to the target and when it is pulled away from the radius, it will not snap back?

Im gonna smile… I just want to show that I wasnt crazy. I think this is a bit easier.

this assumes that you have two circles (your drag object (circle2) and your target object (circle1)). Also, the code is inside the movie clip circle2.

on(press){
this.startdrag();

}
on(release){

if(this.hitTest(_root.circle1)==true){
	
	this._x=_root.circle1._x;
	this._y=_root.circle1._y;
}
this.stopdrag();

}

In keeping with the spirit of MX…

cirlce2.onPress=function(){
this.startdrag();
}

circle2.onRelease=function(){

if(this.hitTest(_root.circle1)==true){

this._x=_root.circle1._x;
this._y=_root.circle1._y;
}
this.stopdrag();
}

yes that would work and its a bit less code but if you use prototype it helps to reduce the code if you are using it on more than one or two instances…

say you had 15 circles and 15 targets… you would have to write that code 15 times instead of

circle1.snap()
circle2.snap()
circle3.snap()
…etc…etc…

Umm… Jubba… It’s me. hehehe… I agree with the use of prototype. I even agree with that proximity thing you guys put together. The intent was to show there was an easier approach.

oh, I figured that you’d agree, but I was just attempting to demonstrate the power of prototypes. Just different approaches reaching the same objectetive. You take the high road and I’ll take the low road… :slight_smile:

Hey, you know me! Happy-Go-Lucky! hehehe… We’re on the same page.

*Originally posted by prankster *
**so… i got lostinbeta’s code to work but i cant drag the clip anywhere else without the clip snapping back to the target. how can i change it so there is only a specific radius where the clip will snap to the target and when it is pulled away from the radius, it will not snap back? **

That problem never occured for me with my method :-\ If I drug the clip out of the radius of the other clip, then it worked just fine dropping it wherever I wanted.

*Originally posted by Jubba *
**yes that would work and its a bit less code but if you use prototype it helps to reduce the code if you are using it on more than one or two instances…

say you had 15 circles and 15 targets… you would have to write that code 15 times instead of

circle1.snap()
circle2.snap()
circle3.snap()
…etc…etc… **
Nope, that’s not true. First of all because your snap prototype doesn’t handle the actual snap: it returns the distance. The snap is done in the onRelease, just like Inigo’s method.
Second thing, Inigo’s method could very well be “prototyped” :slight_smile:

pom :rd:

right I realized that after I wrote that but the snap could be easily prototyped as well.