PDA

View Full Version : Reusing functions for multiple movieclips



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");
}
}

Felixz
May 24th, 2008, 04:02 PM
:)

gem
May 25th, 2008, 01:42 PM
Thanks for your reply! I mostly understand how your example is working but have some questions. I tweaked the file so bTri2 has a different target called bTri2_t, instead of bTri1.

-- I dont see DragDrop.as as the flash file's document class, so how does it know to call DragDrop?

-- As long as my various shapes have a body and border movieclip embedded within them, the DragDrop function will work for everything?

-- Is the 'switch (target)' asking for how many layers the triangle movieclip (or whatever movieclip is being dragged by the mouse) has?

-- What is the difference between dropTarget, dropTarget.parent and parent.parent? I traced all 3 cases and only the dropTarget.parent case is being used.

I'm still pretty new to Flash, so sorry if these are silly questions. =)

-gwen

Felixz
May 25th, 2008, 01:50 PM
1) It is a class in same package as ur library, so it is imported automatically (it is a BaseClass of MCs)
2) Yes, u can change it by checking presence of those clips.
3), 4) I have done that because if clip u have dropped on has children, dropTarget is that children not the clip to compare, so it returns false. There must be a better way to check that

gem
May 26th, 2008, 10:45 AM
Yeah, the dropTarget.parents is definitely giving me trouble, especially because all of the pieces have to fit together snugly. Is there a way to define a specific movieclip/instance name that dropTarget should look for?

I'm also having problems with the rotation check. Right now I'm just checking = or != for .rotation but this has generated wonkyness. I started to set up rotation variables in the main fla file that I could pass to DragDrop.as. Perhaps I could set a Boolean value for when the shape is correctly rotated, and then only let the shape attach if the Boolean is true?

Flash file was too big to attach, here's a link (http://gwenmurray.net/misc/gMurray_final_09.fla)