gem
May 24th, 2008, 02:20 PM
Hello,
I'm making a simple drag and drop game where a shape moveclip snaps into the location of the target movieclip. I have the code working for one shape, but I don't know how to write the functions so they can be used for multiple shapes. Please refer to my flash file and code below.
Since I have nested movie clips, evt.target.startDrag doesn't work for the onStartDrag function because then only the bTri1_d movieclip attaches to the mouse, not bTri1 (which encompasses both the drag and rotate clips). Could I somehow access the root of bTri1_d so it knows to grab the whole thing?
The onSnapTo function is also problematic. I know I can pass the x and y values of the shapes and their targets to the function and use them as variables in the code, but then when I have to removeEventListeners how do I pass the actual name of the movieclip that needs modifying? I tried doing it as a string but that backfired.
Thanks,
-gwen
bTri1.bTri1_d.addEventListener(MouseEvent.MOUSE_DO WN, onStartDrag);
bTri1.bTri1_d.addEventListener(MouseEvent.MOUSE_UP , onStopDrag);
bTri1.bTri1_r.addEventListener(MouseEvent.MOUSE_DO WN, onRotate);
function onStartDrag(evt:MouseEvent):void {
bTri1.startDrag();
}
function onStopDrag(evt:MouseEvent):void {
bTri1.stopDrag();
onSnapTo();
}
function onRotate(evt:MouseEvent):void {
bTri1.rotation += 90;
}
function onSnapTo():void {
if ( (Math.abs(bTri1_t.x - bTri1.x) < 15) && (Math.abs(bTri1_t.y - bTri1.y) < 15) ) {
//trace("i'm right here");
bTri1.x = bTri1_t.x;
bTri1.y = bTri1_t.y;
bTri1.bTri1_d.removeEventListener(MouseEvent.MOUSE _DOWN, onStartDrag);
bTri1.bTri1_d.removeEventListener(MouseEvent.MOUSE _UP, onStopDrag);
bTri1.bTri1_r.removeEventListener(MouseEvent.MOUSE _DOWN, onRotate);
} else {
//trace("i'm too far away");
}
}
I'm making a simple drag and drop game where a shape moveclip snaps into the location of the target movieclip. I have the code working for one shape, but I don't know how to write the functions so they can be used for multiple shapes. Please refer to my flash file and code below.
Since I have nested movie clips, evt.target.startDrag doesn't work for the onStartDrag function because then only the bTri1_d movieclip attaches to the mouse, not bTri1 (which encompasses both the drag and rotate clips). Could I somehow access the root of bTri1_d so it knows to grab the whole thing?
The onSnapTo function is also problematic. I know I can pass the x and y values of the shapes and their targets to the function and use them as variables in the code, but then when I have to removeEventListeners how do I pass the actual name of the movieclip that needs modifying? I tried doing it as a string but that backfired.
Thanks,
-gwen
bTri1.bTri1_d.addEventListener(MouseEvent.MOUSE_DO WN, onStartDrag);
bTri1.bTri1_d.addEventListener(MouseEvent.MOUSE_UP , onStopDrag);
bTri1.bTri1_r.addEventListener(MouseEvent.MOUSE_DO WN, onRotate);
function onStartDrag(evt:MouseEvent):void {
bTri1.startDrag();
}
function onStopDrag(evt:MouseEvent):void {
bTri1.stopDrag();
onSnapTo();
}
function onRotate(evt:MouseEvent):void {
bTri1.rotation += 90;
}
function onSnapTo():void {
if ( (Math.abs(bTri1_t.x - bTri1.x) < 15) && (Math.abs(bTri1_t.y - bTri1.y) < 15) ) {
//trace("i'm right here");
bTri1.x = bTri1_t.x;
bTri1.y = bTri1_t.y;
bTri1.bTri1_d.removeEventListener(MouseEvent.MOUSE _DOWN, onStartDrag);
bTri1.bTri1_d.removeEventListener(MouseEvent.MOUSE _UP, onStopDrag);
bTri1.bTri1_r.removeEventListener(MouseEvent.MOUSE _DOWN, onRotate);
} else {
//trace("i'm too far away");
}
}