PDA

View Full Version : snap to point with hit test?


brewthom
07-15-2008, 09:09 PM
Ugh. This can't be that hard. Maybe I'm missing something simple. I want one mc to snap to the center of another mc when it's within 10 pixels of the target (center of mc). I've got the drag working and the hit test, but not the snap. Ideas? I've simplified here to just include x.


var xNum:int;
xNum=hit.x;


stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function everyFrame(e:Event):void{

if (ball.hitTestObject(hit)){

ball.x=xNum;
trace("works");
stage.removeEventListener(Event.ENTER_FRAME,everyF rame);
}
}

macco
07-16-2008, 02:33 AM
Just add this code:

mc1.x = mc2.x
mc1.y = mc2.y

instead of

trace("works");

Canazza
07-16-2008, 08:08 AM
here's a center function. You'll need to import flash.geom.rectangle if you're putting it in a class
function isAtCenter(mc1:DisplayObject,mc2:DisplayObject,thr eshold:int) {
var bounds1:Rectangle = mc1.getBounds(root)
var bounds2:Rectangle = mc2.getBounds(root)
var center_1:Rectangle = new Rectangle(
(bounds1.left + bounds1.right) / 2 - threshold,
(bounds1.top + bounds1.bottom) / 2 - threshold,
threshold * 2, threshold*2
)
var center_2:Rectangle = new Rectangle(
(bounds2.left + bounds2.right) / 2- threshold,
(bounds2.top + bounds2.bottom) / 2- threshold,
threshold * 2, threshold*2
)
return center_1.intersects(center_2)
}

brewthom
07-31-2008, 09:23 PM
a belated thank you!