A Flash Developer Resource Site

Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Jan 2012

    2 problems: drag and drop and an easier question

    So I'm making an app for a class I'm in where there is a list of courses in a scroll bar on the side of a screen and you drag and drop those movie clips into spots on a time planner. I have a drag and drop class set up and they are all dragging fine. I just need to know how to get the movie clips for each of the course the ability to snap into each empty space.

    I can do like this:

    aComm2.target = selectionPage.courseTarget;

    courseTarget is my transparent movieclip I am using a target and aComm2 is the name of the course. It will snap there but I have a LOT more courses and about 24 empty spaces. What is the best way to set it up so any course can be drag and dropped into any space.

    Last thing I need a little help with shouldn't be that hard. When you addChild a movieClip what code do I need to write so it is add in the exact location as my mouse?

    thanks a lot guys.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    This is just an idea, but if you've got a system where you have a long list of draggable objects and a long list of drop spots, you would create a function that sets it up, and then for the number of drag and drops you have, you just run through that function however many times.

    I don't know if it's the best way to go about it (still on the road of learning myself) but what could work is this:

    var dragMovieClips:Array = [drag1, drag2, drag3]; //Array of all drag MovieClips
    var dropMovieCilps:Array = [drop1, drop2, drop3]; //Array for all drop MovieClips
    //This for loop runs through however many drag and drop objects you have 
    //(it's based on the number of drag objects, however)
    for(var i:int=0; i<dragMovieClips.length; i++){
    	setUpDrag(dragMovieClips[i], dropMovieCilps[i]); //Run the function that sets up each drag and drop instance
    //Function that sets up each drag and drop instance
    function setUpDrag(dragMc:MovieClip, dropMc:MovieClip):void{
    	//Put other drag and drop code here
    	//Such as aComm2.target = selectionPage.courseTarget;
    	dragMc.target = selectionPage.dropMc;
    What I'm doing is I have two arrays, once with all the drag MovieClips, and another with all the drop MovieClips. Both arrays are in order as well, so there is a small amount of setting up to make sure nothing stuffs up.

    Then I do a for loop for however many MovieClips are in the dragMovieClip array (in this case, it runs 3 times). For each time the loop fires, it sets up a pair of drag and drop items based on the current position of the loop. The variable i in the loop starts at 0, and will finish at 2, so I use this as a way to target each item in both arrays by using it as the index: dragMovieClip[0] returns drag1, so dragMovieClip[i] will return whatever i is equal to.


    For the other question, when you add a child and want it's coordinates to be that of the mouse, just use mouseX and mouseY:

    ball.x = mouseX;
    ball.y = mouseY;
    Just take note of the ball's registration point as you add it. If you want the ball's centre to be on the mouse when it's added, make sure the registration point of the ball is set to the centre, and not the top left (or anything else).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.