11 Replies Latest reply on Oct 31, 2009 4:26 PM by kglad

    Scripts referencing their own MC?

    Desprez10 Level 1

      How can you get a script on a clip to reference its own clip?

      'this' seems to only return the path up to, but not incuding the button.

       

      For example, say I want a movieclip to delete itself when clicked, but I don't know the name of the clip:

      (Or self-reference anything in a dynamicaly created MC)

       

      on (release) {

           this.removeMovieClip();

      }

       

      Obviously, this doesn't work.

       

      I've worked around this problem by inserting the script on a transparent MC inside the target clip - on top of any other goodies that happen to be in the clip. This gives me a layer to click that doesn't hide the other stuff, and 'this' refers to the correct item to delete, letting the above script work.

       

      But this seems a clunky way to do it, and I think the transparent layers are causing a performance hit.

      What's a better way to go about this?

        • 1. Re: Scripts referencing their own MC?
          kglad Adobe Community Professional & MVP

          that will work if it's attached to a movieclip with a depth greater than or equal to zero.  ie, use swapDepths().

          • 2. Re: Scripts referencing their own MC?
            Desprez10 Level 1

            I don't see how that applies to the situation. Can you please elaborate?

            • 3. Re: Scripts referencing their own MC?
              kglad Adobe Community Professional & MVP

              on (release) {

              this.swapDepths(this._parent.getNextHighestDepth());

                   this.removeMovieClip();

              }

              • 4. Re: Scripts referencing their own MC?
                Desprez10 Level 1

                Maybe I'm missunderstanding what is happening here.

                 

                Say I have:

                _root.drawer.socks.money1

                where the script resides in the money1 MC.

                I want to remove the money1 clip when clicked.

                 

                referencing from inside money1:

                this  = _level0.drawer.socks

                _parent = _level0.drawer

                this._parent = _level0.drawer

                 

                so

                this.swapDepths(this._parent.getNextHighestDepth());

                gives _root.drawer.socks a higher depth than it used to have, but it is still the parent of .money1, no? How does this reference money1?

                If you're saying that getNextHighestDepth() will get the highest depth in the whole hierarchy, thus making money01 and socks switch parent and child roles, I haden't realized that was possible. Still, wouldn't then 'this' = _level0.drawer ?

                • 5. Re: Scripts referencing their own MC?
                  kglad Adobe Community Professional & MVP

                  are you attaching code directly to a movieclip?  if so, then the code i gave will work unless you're using certain v2 components or you're publishing for flash 6 or earlier.

                  • 6. Re: Scripts referencing their own MC?
                    Desprez10 Level 1

                    It doesn't work. It deletes the movie clip containing the clip I wanted to delete.

                     

                    I'm using flash 8, I don't know what v2 components you are refering to - I don't think I'm using any?

                    • 7. Re: Scripts referencing their own MC?
                      kglad Adobe Community Professional & MVP

                      then you're not attaching that code to a movieclip.

                       

                      read my original message:  "that will work if it's attached to a movieclip with a depth greater than or equal to zero.  ie, use swapDepths()."

                      • 8. Re: Scripts referencing their own MC?
                        Desprez10 Level 1

                        If by 'attached' you mean the code needs to be inside a clip, then yes, it's attached.

                        Did you mean something else? Or does 'attached' have a specific technical meaning regarding Flash that differs from common usage?

                         

                        I traced the depth of the clip and apparenty it has a negative depth. I don't really know what that means. By your responses, I'm assuming that I can't just change it to positive?

                        • 9. Re: Scripts referencing their own MC?
                          kglad Adobe Community Professional & MVP

                          you're attaching your code to a button, not a movieclip.

                          1 person found this helpful
                          • 10. Re: Scripts referencing their own MC?
                            Desprez10 Level 1

                            That seems to have been the problem.

                            I also noticed that buttons and movieclips trace differently.

                            for the button, this = _level0.drawer.socks

                            for the movieclip, this = _level0.drawer.socks.money1

                             

                            So naturaly my next question is why is the depth swapping needed in the first place if movieclips can reference themselves with 'this'?

                            The trace was the same before and after the the depth swapping.

                            • 11. Re: Scripts referencing their own MC?
                              kglad Adobe Community Professional & MVP

                              first, removeMovieClip() is a movieclip method.  to have it work for buttons you need to extend the Button class so it has that method:

                               

                              Button.prototype.removeButton = MovieClip.prototype.removeMovieClip;

                               

                              second, you can't use actionscript to remove an object from a depth less than 0.  so, for objects created in the authoring environment (that are assigned negative depths by flash, you must use swapDepths() to move the object to a removeable depth.

                               

                              again, because swapDepths() is a movieclip method, to have it work for buttons, you need to extend the Button class

                               

                              Button.prototype.swapDepths=MovieClip.prototype.swapDepths;

                               

                              because buttons have no timeline and "this" references the local timeline, when attached to a button, "this" will reference the timeline that contains the button.  when attached to a movieclip button, "this" will reference the movieclip.

                               

                              and finally, don't ever use buttons.  use movieclip buttons.  you'll save yourself a lot of headaches with very little downside other than saving less than 2 minutes coding for over and down frames.

                              1 person found this helpful