2 Replies Latest reply on Mar 14, 2008 11:38 AM by Rothrock

    setting masks with AS

    brian914 Level 1
      What am I doing wrong?

      I have a MC in my library with a linkage identifier of "mask". And I have a bunch of MCs on my stage with a instance names of line_0, line_1, line_2.... etc.
      Somehow the mask it not getting set. What am I doing wrong?

      for (var i:Number = 0; i < numOfLines; i++){
      this.attachMovie("mask", ["mask_" + i], this.getNextHighestDepth(), {_x:647, _y:1165});
      this["line_" + i].setMask("mask_" + i);
      }

      Thanks a lot for any help!
        • 1. Re: setting masks with AS
          Greg Dove Level 4
          It might be easier to have all your line clips inside a single parent clip and then you can just apply the mask to the parent clip... but if not then maybe try it like this:

          for (var i:Number = 0; i < numOfLines; i++){
          var masker:MovieClip= this.attachMovie("mask", "mask_" + i, this.getNextHighestDepth(), {_x:647, _y:1165});
          this["line_" + i].setMask(masker);
          }
          • 2. Re: setting masks with AS
            Rothrock Level 5
            I think the problem is with your attachMovie line, specifically the second argument. That is where you pass in the name of the newly attached clip. It doesn't need the array brackets around it.

            However your setMask line where you are passing in the name of the recently attached mask clip will need them!

            this["line_"+i].setMask(this["mask_"+i]);

            That is because in that case you need to tell flash to look inside the current timeline (this) and look for the item named "mask_"+i. Get it? When you give the name, it only needs a string for the name, because you are telling it where to attach with the this before the attachMovie. But afterwords you have to tell flash where to look so then you do need the array notation.

            BUT, there is a shortcut that is very useful.

            var curLine:MovieClip=this["line_"+i];
            var curMask:MovieClip=this.attachMovie("mask","mask"+i,100+i,{yourobject});
            curLine.setMask(curMask);

            By defining temporary variables to be your line and your mask, you can make the code easier to read and manage. Notice the first one I just use the same code you've got, but assign it to a variable. This is escpecially good if you are going to use that several times. I don't know about you, but I have trouble always typing the square brakets, the quotes, the plus and the variable in the right order without a type. In this case you type it once and then use the much easier to read and type curLine over and over.

            In the second line, I rely on a how attachMovie works. If you look it up in the help files, there is a line where it will tell you that attachMovie returns a reference to the movie it just attached. In fact many methods return things and you can either ignore them or use them if you need them. In this case I capture that return and assign it to curMask. Then again, I can always be sure that I've got the correct mask by using the simple curMask.

            Tricky, huh?