11 Replies Latest reply on Aug 20, 2007 1:35 PM by kglad

    Does a listener still exist after its target object MC has been removed?

    ritpas
      Hi,

      I have just a question about as3.0 programming (I suppose it is the same for AS 2.0):
      When a MovieClip is removed from the display list (either by removeChild method or directly from within the stage from the timeline), if listeners are running and attached to that object, are they also removed, or do I have to remove them explicitly?

      if yes, for a proper programming, I suppose that I have to program a listener for the UNLOAD Event whose function will remove all attached listeners, don't I?
        • 1. Re: Does a listener still exist after its target object MC has been removed?
          kglad Adobe Community Professional & MVP
          you must do more than just remove a movieclip from the display list to remove it from your swf (ie, to have it garbage collected).

          you must remove all strong references to it (which include listeners unless you explicity make the listener a weak reference) and you must assign the movieclip to null.
          • 2. Re: Does a listener still exist after its target object MC has been removed?
            ritpas Level 1
            In fact, I am experiencing a strange thing : I have a lot of MC (added dynamically to the stage, they are linked to a library id and so belong to a class), when I remove one by using parentclip.removeChild(clipname), the clip is indeed removed from the stage but it looks as if its timeline keeps playing...

            I don't know if I am clear in my explanantions ...

            So, according to you, to remove it, I have not only to use the removeChild method but also setting the MC's value to null like that :
            parentclip.removeChild(clipname);
            clipname=null;
            • 3. Re: Does a listener still exist after its target object MC has been removed?
              ritpas Level 1
              to be clearer, I detail my problem :
              I am programming a diapo show : 3 clips called diapo1, diapo2 and diapo3 having each their own timeline are appearing one after the other : at the end of each timeline, the next clip is called, appearing with a fade in effect, overlaying the previous clip.
              Suppose diapo2 is at its end, and calls diapo3 and so the transition effect (alpha value of diapo2 decreases whereas alpha value of diapo3, initially set to 0, increases), I made the following condition in my transition effect :
              if the alpha value reaches 0 (it means that the overlayed clip has reached 1), I remove the underlayed clip by using removeChild function but to make it properly work, I have also to apply the stop method to make my underlaying clip stop playing its timeline.
              I thought that removeChild would remove it and so stop playing, but obviously it is not the case!

              To me, it is not logical....
              • 4. Does a listener still exist after its target object MC has been removed?
                ritpas Level 1
                OK, PLEASE FORGET all I wrote above, I recreate the problem by writing some easy code lines.

                Consider the following :

                I create a new empty .fla file called test.fla, I have a clip in my library called MyClip and linked to the following class :
                package{
                import flash.display.ovieClip;
                public class MyClip extends MovieClip {

                }
                }
                MyClip contains 20 empty frames in its timeline, at frame 20, I add trace("playing!");

                under the main timeline, I have the following code :
                var clip:MyClip=new MyClip();
                this.addChild(clip);
                this.removeChild(clip);
                clip=null;
                trace(clip);

                => when RUNNING it, I get the following in my output panel :
                null
                playing!
                playing!
                ...
                I get "playing!" each 20 frames indefinetely though I "removed" the clip from the displayList.

                Is such a behaviour normal? Why?
                How do I do to "completely" remove it?

                thanks


                • 5. Re: Does a listener still exist after its target object MC has been removed?
                  Rothrock Level 5
                  It is called the DISPLAYlist and not the EXISTENCElist for a reason. :)
                  • 6. Re: Does a listener still exist after its target object MC has been removed?
                    kglad Adobe Community Professional & MVP
                    that behavior is normal. as rothrock suggested and my above message stated, you must null the movieclip to make it eligible for garbage collection.

                    being eligible for garbage collection (gc) means it will be removed from existence when memory usage of your swf increase "enough" to trigger gc.
                    • 7. Re: Does a listener still exist after its target object MC has been removed?
                      ritpas Level 1
                      quote:

                      It is called the DISPLAYlist and not the EXISTENCElist for a reason. :)

                      => yes sure :)

                      quote:

                      being eligible for garbage collection (gc) means it will be removed from existence when memory usage of your swf increase "enough" to trigger gc.


                      => ok, I understand, but is there not any way to invoke gc, to trigger it manually?
                      If you retake my example above, "playing!" seems to display indefinetely, so it means that gc has not been triggered which may be normal because memory hasn't increase dbut it is not handy because the code in the clip supposed to be removed is still running (even when clip is set to null). I persist and still don't think it is normal :)

                      But I will study a lot more as 3.0, I think I should find an answer in the interesting Colin Moock's book I am currently reading, I am just at the beginning.

                      Thanks for your help!
                      • 8. Re: Does a listener still exist after its target object MC has been removed?
                        kglad Adobe Community Professional & MVP
                        there is no "clean" way to trigger gc and there's no precise way to trigger gc. you can trigger gc by adding "stuff" to your swf so it occupies more memory and thereby triggers gc.

                        for testing i start a loop that repeatedly adds movieclips to the swf.
                        • 9. Re: Does a listener still exist after its target object MC has been removed?
                          Rothrock Level 5
                          gc will happen when it happens. So evidently the "easy" answer is just like you need to remove listeners or clearIntervals before deleting, nulling, removing, etc. things. You will need to:

                          clip.stop();

                          Before you null it. Seems like a step backwards in many ways, but…
                          • 10. Re: Does a listener still exist after its target object MC has been removed?
                            ritpas Level 1
                            Yes, it seems really like a step backwards as if something has been forgotten...

                            clip.stop();
                            => I did it, no choice, but it is a little bit cumbersome

                            Anyway it is paradoxical, for example, take the "visible" property of a movieClip, you set it to true and its timeline is stopped whereas here, not only you make it unvisible but you remove it from the display list, and still its timeline is playing !!!
                            • 11. Re: Does a listener still exist after its target object MC has been removed?
                              kglad Adobe Community Professional & MVP
                              "...take the "visible" property of a movieClip, you set it to true and its timeline is stopped..."

                              where do you see that?