Recursive event handlers?

Hi,
I'm creating a game and need to drag-and-drop a movie clip while creating a duplicate on the same spot so the dragging can be done many times. This is approximately what I'm trying to do:

mc_num = 0;
mc0.onPress = doDrag;
mc0.onRelease = doDrop;

      function doDrag():Void {
         mc_num++;
         mc = this.duplicateMovieClip("mc"+mc_num, this.getNextHighestDepth());

        mc.onPress = doDrag;
        mc.onRelease = doDrop;
          this.startDrag();
      }

The problem is, looks like this kind of recursion doesn't work, so the new clip turns out to be not draggable. Is there any workaround here?
jgenyukAsked:
Who is Participating?
 
Aneesh ChopraCommented:
Ok, here the code

open a new FLA,

create a new movieClip, place an instance on stage, name it "mc"


and place the following code on first framemee::
------
mc.onPress = createNew;
mc.onRelease = mc.onReleaseOutside=function ()
{
      stopDrag();
};
//
function createNew()
{
      mc_num++;
      newMc = mc.duplicateMovieClip("mc"+this._parent.getNextHighestDepth(), this._parent.getNextHighestDepth());
      startDrag(newMc);
      newMc.onPress = function()
      {
            startDrag(this);
      };
      newMc.onRelease = newMc.onReleaseOutside=function ()
      {
            stopDrag();
      };
}

----------

Rgds
Aneesh
0
 
MontoyaProcess Improvement MgrCommented:
if you attach an instance of the movie from the library that already has that behavior coded, every instance will include the drag and drop behavior.

Do you know what I mean?

Montoya
0
 
jgenyukAuthor Commented:
Montoya,
Thanks but this doesn't seem to work either. If I move the event handlers to the movie in library, the first instance works fine, but the one created at runtime doesn't drag. Tracing shows that the events handlers do get called so I'm completely mystified here.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Aneesh ChopraCommented:
Hi,


Let's assume your main movieClip instancename is "mc0"

Just replace your complete code with the following, it will work for you:
---------
mc_num = 0;
mc0.onPress = createNew;
mc0.onRelease = mc0.onReleaseOutside=function ()
{
      stopDrag();
};
//
function createNew()
{
      mc_num++;
      mc = mc0.duplicateMovieClip("mc"+mc_num, this._parent.getNextHighestDepth());
      startDrag(mc);
}
-----------

Rgds
Aneesh
0
 
Aneesh ChopraCommented:
I have also uploaded a working sample for you which uses the same code as I suggested above

download it here:
http://download.yousendit.com/113993BE2571C7BC

Rgds
Aneesh
0
 
jgenyukAuthor Commented:
Thanks, Aneesh, but in your sample the new movie clips do not inherit the dragging behavior, which is exactly my problem.
0
 
Aneesh ChopraCommented:
"inherit the dragging behavior" ?

could u please explain this point....?
0
 
jgenyukAuthor Commented:
The newly created instances cannot be dragged again.
0
 
jgenyukAuthor Commented:
..once they are dropped, I mean.
0
 
Aneesh ChopraCommented:
you mean to say, stage will have one movieClip, user drag it and a duplicate movieClip comes out of it...

now if user try to drag the newly created movieClip, it should also create a duplicate of itself and the new one should be dragged out..

if I understood it correctly, please confirm....
i will update the sample
0
 
jgenyukAuthor Commented:
The new clip doesn't have to duplicate itself, it just has to be draggable as many times as the user feels like it. To explain better, this is a game where player creates new instances of an object by dragging it on the gameboard and then moves them around.
0
 
Aneesh ChopraCommented:
I have also uploaded the new sample here:
http://download.yousendit.com/32BC81671AC330E4

Rgds
Aneesh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.