8 Replies Latest reply on May 23, 2008 8:00 AM by Bob Pierce

    Using DepthManager with createEmptyMovieClip

    Bob Pierce Level 1
      The AS Language Reference tells me to use DepthManager rather than MovieClip.getNextHighestDepth() when using version 2 components. But the two seem to operate so differently that I can't see how to replace one with the other in cases like this:

      this.createEmptyMovieClip("container", this.getNextHighestDepth());

      It seems that DepthManager references the mc whose depth is to be set whereas getNextHighestDepth references its parent eg.

      container.setDepthTo(DepthManager.kTop);

      So what should replace this.getNextHighestDepth() in createEmptyMovieClip?

        • 1. Re: Using DepthManager with createEmptyMovieClip
          robdillon Most Valuable Participant
          You can use this.setDepthTo(DepthManager.kTop); instead of this.getNextHighestDepth(); and you will achieve the same result.
          • 2. Re: Using DepthManager with createEmptyMovieClip
            Bob Pierce Level 1
            I'm afraid your solution does not work although it seems to have been the official line for some time! The attached code demonstrates the problem and a workaround.

            While not in the conspiracy of silence category this does seem to have been a long term blind spot. The Flash 8 LiveDocs for createEmptyMovieClip() has an entry in September 2006 highlighting the problem but it was not acknowledged.

            The difficulty is quite obvious: createEmptyMovieClip() and other methods like it require a number to be supplied for the depth. The method getNextHighestDepth() returns a number, albeit a flaky one in the presence of version 2 components. DepthManager does not return a number. Instead it does exactly what it says in the documentation; mc.setDepthTo(DepthManager.kTop) sets the depth of mc to 20 places above the currently highest occupied depth. DepthManager is a method of the target mc whereas getNextHighestDepth() is a method of mc's parent. You can't invoke a method of movieclip that hasn't been created yet.

            So there is no way that DepthManager can be used as a simple replacement for getNextHighestDepth() and it seems quite cynical that the documentation is sprinkled with recommendations to do just that without any explanation or example of how it can be done. The DepthManager documentation is particularly short on code examples compared with other areas.
            • 3. Re: Using DepthManager with createEmptyMovieClip
              robdillon Most Valuable Participant
              Hmmm...

              I see your point and in running your code I see the problem that you describe. I just looked at a program that uses this method, both for components and for movieClips. It works and it works correctly. I just tested the depth that was returned by a movieClip and the result was correct. It may be that you need to have at least one component whose depth is set using the depth manager before you set the depth of a movieClip.
              • 4. Re: Using DepthManager with createEmptyMovieClip
                Bob Pierce Level 1
                I don't think it's as simple as that....

                • 5. Re: Using DepthManager with createEmptyMovieClip
                  robdillon Most Valuable Participant
                  I don't know what to do with the code that you sent. I don't see any reference to a component there. Here's an abbreviation of the code that I used to instantiate one of the component types:
                  • 6. Re: Using DepthManager with createEmptyMovieClip
                    Bob Pierce Level 1
                    Sorry, I used onStageBtn to refer to a button component I'd placed on stage.

                    If your code works for you I don't understand how, unless your code resides on a child timeline - see below. Within the function below, "this" traces _level0 so I wouldn't expect a sensible result. That aside, even if it did work, this example misses the point. In situations where a new object is being created and a depth has to be supplied, the method MC.getNextHighestDepth() returns a number representing the next free depth in MC which is the parent of the object being created. This method cannot be simply replaced by any method of the DepthManager class as they do not return a number. The method setDepthTo() only operates on an existing instance and therefore cannot be used to specify the depth in methods such as createEmptyMovieClip().
                    • 7. Re: Using DepthManager with createEmptyMovieClip
                      robdillon Most Valuable Participant
                      I think I'm seeing the difference between what I did and what you're doing. I'm not trying to set a depth for an empty movieClip. I'm loading an .swf into that clip and then, when I position the clip, I give it a new depth. Have a look at this example building on your example. Substitute your own .swf to load in.
                      • 8. Re: Using DepthManager with createEmptyMovieClip
                        Bob Pierce Level 1
                        Thank's, I think that's clear now. Getting back to my original question: what should replace this.getNextHighestDepth() in createEmptyMovieClip? The answer appears to be "1", or whatever you choose, but followed by a DepthManager call. Eg

                        this.createEmptyMovieClip("newMC", 1);
                        newMC.setDepthTo(DepthManager.kTop);