8 Replies Latest reply on Oct 9, 2007 7:19 AM by ferdinand berfel

    as 2.0 class objects- how to swap depths of a movie clip

    ferdinand berfel
      How do you bring an object to the top? if it's just a movie clip, I could do a swapdepths, but if it's a movieclip that's part of an AS 2.0 object, how do you swap depths of the whole object?

      I create 2 objects (same class) which each have a movieclip within them. The movie clip is created on a unique level with getNextHighestDepth().

      I have a button which tries to swapDepths of the 2 objects, but I can't get it to work. Can anyone help?


      here's the detail:

      1. create a symbol in the library called "someShape_mc" and put some shape in it - a circle, a square, whatever - this symbol is exported for action script, and has an AS 2.0 Class of "ClassObject" ( I also put a dynamic text field in the shape to display the current depth - it's called "depth_txt")

      2. create a button called "swap_btn" on the stage.

      Frame 1 has the following actionscript:

      var BottomObject:ClassObject = new ClassObject(this,100,150);
      var topObject:ClassObject = new ClassObject(this,110,160);

      // for the button add this:

      Swap_btn.onRelease=function() {

      // try it with the full path:
      _root.BottomObject.__LocalMovieClip.swapDepths(_root.topObject.__LocalMovieClip);

      // try it with with just the objects:
      BottomObject.__LocalMovieClip.swapDepths(topObject.__LocalMovieClip);

      // try it with the object as a movieclip
      BottomObject.swapDepths(topObject);
      trace("Did it Swap?");

      // try it with a method in the class....
      BottomObject.swapIt(topObject.__LocalMovieClip);
      BottomObject.swapIt(topObject);
      trace("nope... no swapping going on...");
      }

      ================================

      here's the AS file: "ClassObject.as"

      class ClassObject extends MovieClip{
      var __LocalMovieClip;
      var __Depth;
      function ClassObject(passedIn_mc:MovieClip,x:Number,y:Number) {
      __Depth = passedIn_mc.getNextHighestDepth();
      __LocalMovieClip = passedIn_mc.attachMovie("someShape_mc","__LocalMovieClip",__Depth);
      trace("made a shape at " + __Depth);

      __LocalMovieClip._x = x;
      __LocalMovieClip._y = y;
      __LocalMovieClip.depth_txt.text = __Depth;

      }

      public function swapIt(targetMc) {
      __LocalMovieClip.swapDepths(targetMc);
      __LocalMovieClip.depth_txt.text = __LocalMovieClip.getDepth(); // no difference.
      trace("Tried to swap from within the class...");
      }
      }


      ========================
      so- the goal is to bring the "bottom" Class object on top of the "top" object. The button tries various methods of swapping the depths of the movie clips - but there is not one that works. What am I missing?

      tia
      ferd
        • 1. Re: as 2.0 class objects- how to swap depths of a movie clip
          kglad Adobe Community Professional & MVP
          the appearance of the two movieclips depends upon the relative depth of the most distant parent of the two movieclips that's not the same.

          eg, if each movieclip has a different parent (and those parents are children of the same timeline), then their appearance depends upon the depth of the parents, not their depths.
          • 2. Re: as 2.0 class objects- how to swap depths of a movie clip
            DW@Peel
            Here's a quick analogy:

            You have two boxes stacked on top of each other.
            Each box contains a pancake.

            Note: In AS 2.0 you cannot take the pancake out of the box.

            To place the bottom most pancake on the top you'd need to place the box it's contained within on top of the other box.
            • 3. Re: as 2.0 class objects- how to swap depths of a movie clip
              ferdinand berfel Level 1
              I like the box analogy. so now the question is, how do I place the bottom most box on top of the topmost box? - given my AS file above - I presume the "box" to be the class object - how do I change the position of that box?

              tia
              • 4. Re: as 2.0 class objects- how to swap depths of a movie clip
                kglad Adobe Community Professional & MVP
                use swapDepths on the _parent movieclips.
                • 5. as 2.0 class objects- how to swap depths of a movie clip
                  DW@Peel Level 1
                  Your ClassObject is extending a MovieClip and is therefore a movieclip itself.
                  What you need to do is instantiate this class not by calling:
                  new ClassObject();
                  but rather by attaching the MovieClips like so:
                  var BottomObject:ClassObject = ClassObject(_root.attachMovie(linkageID, depth));

                  From the looks of things you don't need to have those containers at all. You're using the ClassObject both on it's own and attaching it to your movieclip.

                  Try this:
                  ////////////////////////////////
                  // Frame 1
                  ///////////////////////////////

                  var bottomObject:ClassObject = ClassObject(this.attachMovie("someShape_mc", "bottom_mc", 0, {_x:100, _y:150}));

                  var topObject:ClassObject = ClassObject(this.attachMovie("someShape_mc", "top_mc", 1, {_x:110, _y:160}));

                  Swap_btn.onRelease=function() {
                  bottomObject.swapIt(topObject);
                  // bottomObject.swapDepths(topObject) is also perfectly valid
                  // the reason I opted for the function within the class is to allow your
                  // text field to register the depth
                  }


                  /////////////////////////////////
                  // ClassObject.as
                  /////////////////////////////////
                  class ClassObject extends MovieClip{

                  private var depth_txt:TextField;

                  function ClassObject() {
                  displayDepth();
                  }

                  public function swapIt(target_mc:MovieClip):Void {
                  this.swapDepths(target_mc);
                  displayDepth();
                  }

                  private function displayDepth():Void{
                  depth_txt.text = String(this.getDepth());
                  }
                  }

                  Note:
                  I just fixed a few typos and tested to make sure my code wasn't wacky. This should work for you.
                  Enjoy!
                  • 6. Re: as 2.0 class objects- how to swap depths of a movie clip
                    ferdinand berfel Level 1
                    Thank you for your response - and here I have included the code I reworked to show how it works, and doesn't work. you're right about not needing the extra containers, but this example is part of a bigger thing...

                    I'm confused - it works ONLY if I attach the movie outside the class, even though the "attachment" occurs, I'm thinking, at the same scope level, that is, _root.holder_mc, in both examples.

                    it seems that the advantage of having a class is defeated since I have to do the extra coding for each object that will be created. It's like the class can only have a reference to the movieclip outside itself, and not have a clip INSIDE that is fully functioning. am I right about this? Is there someplace good I can learn more about class objects and movieclip usage?

                    also, my class object IS a movieclip, but " this.getDepth() " is meaningless inside the class object. hmmm...



                    This one works..... attaching the movies at the root level (to a holder_mc)

                    ////////////////////////////////
                    // Frame 1
                    ///////////////////////////////
                    tmp1 = holder_mc.attachMovie("someShape_mc","tmp1",holder_mc.getNextHighestDepth());
                    var BottomObject:ClassObject3 = new ClassObject3(tmp1,100,150);


                    tmp2 = holder_mc.attachMovie("someShape_mc","tmp2",holder_mc.getNextHighestDepth());
                    var topObject:ClassObject3 = new ClassObject3(tmp2,110,160);

                    // for the button add this:

                    Swap_btn.onRelease=function() {
                    BottomObject.swapIt(topObject);
                    trace("clicked button");
                    }




                    /////////////////////////////////
                    // ClassObject3.as
                    /////////////////////////////////
                    class ClassObject3 extends MovieClip{
                    var __LocalMovieClip:MovieClip;

                    function ClassObject3(passedInMovieClip:MovieClip,x:Number,y:Number) {
                    trace(" this class object is at ["+this.getDepth()+"]");
                    __LocalMovieClip = passedInMovieClip;
                    __LocalMovieClip._x = x;
                    __LocalMovieClip._y = y;
                    }

                    public function swapIt(targetMc:MovieClip):Void {
                    trace("do the swap in the class");
                    trace("===========================");
                    trace("target type :" + typeof(targetMc));
                    trace("__LocalMovieClip type :" + typeof(__LocalMovieClip));
                    __LocalMovieClip.swapDepths(targetMc.__LocalMovieClip);
                    }
                    }

                    This one does NOT work..... attaching the movies within the class object...

                    ////////////////////////////////
                    // Frame 1
                    ///////////////////////////////
                    var BottomObject:ClassObject2 = new ClassObject2(holder_mc,100,150);
                    var topObject:ClassObject2 = new ClassObject2(holder_mc,110,160);

                    // for the button add this:

                    Swap_btn.onRelease=function() {
                    BottomObject.swapIt(topObject);
                    trace("clicked button");
                    }



                    /////////////////////////////////
                    // ClassObject2.as
                    /////////////////////////////////
                    class ClassObject2 extends MovieClip{
                    var __LocalMovieClip:MovieClip;

                    function ClassObject2(passedInMovieClip:MovieClip,x:Number,y:Number) {
                    __LocalMovieClip = passedInMovieClip.attachMovie("someShape_mc","stuff1",passedInMovieClip.getNextHighestDep th());
                    __LocalMovieClip._x = x;
                    __LocalMovieClip._y = y;
                    }

                    public function swapIt(targetMc:MovieClip):Void {
                    trace("do the swap in the class");
                    trace("===========================");
                    trace("target type :" + typeof(targetMc));
                    trace("__LocalMovieClip type :" + typeof(__LocalMovieClip));
                    __LocalMovieClip.swapDepths(targetMc.__LocalMovieClip);
                    }
                    }

                    • 7. Re: as 2.0 class objects- how to swap depths of a movie clip
                      DW@Peel Level 1
                      You're currently problem is a rather simple one and easy to overlook.
                      You are placing two objects on the timeline with the exact same name: "stuff1"

                      place this in your code instead of the line referencing "stuff1"
                      var newDepth:Number = passedInMovieClip.getNextHighestDepth());
                      var clipName:String = "stuff_"+newDepth;
                      __LocalMovieClip = passedInMovieClip.attachMovie("someShape_mc",clipName,newDepth);


                      You owe me. ;)


                      • 8. Re: as 2.0 class objects- how to swap depths of a movie clip
                        ferdinand berfel Level 1
                        aHA! I got it! thank you so much - I DO owe you! good eye!