11 Replies Latest reply on Dec 14, 2008 3:55 PM by whispers

    inverse mask script AS3 > AS2

    gunforhire
      I have a project that has been built in AS2 but I need to create an Inverse Mask.
      I can do it in AS3 with:

      clipToMask.cacheAsBitmap = true
      clipToMask.mask = inverseMask(mask_mc)

      function inverseMask(mc:MovieClip) {
      var _BMP:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFFFFFFFF)
      var invert:ColorTransform = new ColorTransform(0,0,0,1)
      var matrix:Matrix = new Matrix()
      matrix.translate(mc.x,mc.y)
      _BMP.draw(mc, matrix,invert)
      _BMP.threshold(_BMP,new Rectangle(0,0,_BMP.width,_BMP.height),new Point(0,0),"<",0xFFFFFFFF,0x00FF0000)

      var BMP:Bitmap = new Bitmap(_BMP)
      var maskMC:MovieClip = new MovieClip()
      maskMC.addChild(BMP)
      maskMC.cacheAsBitmap = true
      this.addChild(maskMC)
      mc.visible = false
      return maskMC
      }


      BUT.....
      I need to convert this to AS2. The project is almost built, so rewriting the whole job in AS3 isn't an option any longer.
      Can anyone help?
      So far I have (see below) but it doesn't work.


      import flash.display.BitmapData;
      import flash.filters.BitmapFilter;
      import flash.filters.ColorMatrixFilter;
      import flash.geom.ColorTransform;
      import flash.geom.Matrix;


      clipToMask.cacheAsBitmap = true
      clipToMask.setMask(inverseMask(mask_mc));

      function inverseMask(mc:MovieClip) {
      trace("mask: " + mc);

      var _BMP:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFFFFFFFF)
      var invert:ColorTransform = new ColorTransform(0,0,0,1)
      var matrix:Matrix = new Matrix()
      matrix.translate(mc.x,mc.y)
      _BMP.draw(mc, matrix,invert)
      _BMP.threshold(_BMP,new Rectangle(0,0,_BMP.width,_BMP.height),new Point(0,0),"<",0xFFFFFFFF,0x00FF0000)



      var BMP:BitmapData = _BMP.clone();
      var maskMC:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
      maskMC.attachBitmap(clonedBitmap, this.getNextHighestDepth());
      maskMC.cacheAsBitmap = true;
      mc._visible = false
      return maskMC;
      }
        • 1. Re: inverse mask script AS3 &gt; AS2
          kglad Adobe Community Professional & MVP
          change stage.stageWidth and stage.stageHeight to as2 (Stage.width and Stage.height) and x and y properties to _x and _y.
          • 2. Re: inverse mask script AS3 &gt; AS2
            kglad Adobe Community Professional & MVP
            and the last code snippet wouldn't work in as3. it looks like that last BMP declaration should declare clonedBitmap.
            • 3. Re: inverse mask script AS3 &gt; AS2
              gunforhire Level 1
              Cheers!
              AS2 code should look like this:
              • 5. inverse mask script AS3 &gt; AS2
                whispers
                this is an AWESOME function... thanks.. i was looking at how I could accomplish this, but wasn't sure how to go about using the bitmap class to do so.. (not much experience with it I'm afraid)

                I do have a question though....that maybe you can help or suggest a direction for me to pursue.


                I am using function in/on a several times nested clip.

                I am loading an image dynamically, (upon loadInit() I am then attaching that image as a bitmap so it scales nicer than just loaded .jpg)

                I have my mask clip nested as well, but above the imageContainer clip..

                all these nested clips, are under a mask on its own..(so that any image loaded that may be bigger then my display area is masked, and not bleeding over)... however if this nested clip is masked..and I use this function on the clips inside of this container.. it does work correctly..

                Any ideas?

                taking the parent mask away/off it seems to work *fine (sorta)..

                the last problem I'm having is..

                once my loaded image completes.. I have a loadInit() fire form the MovieClipLoader() instance.. so its centered.. and when I scale it.. it scale from the center..

                however, when trying to use the inverse mask method above.. the mask is created 'not' where the mask clip its copying is located.. its off-set for some reason..

                the layout is this:

                parent clip--
                -----lineContainer (holes the shape/object Im using for my source of the inverse mask
                -----maskContainer (empty clip to hold the dynamically created clip form the inverse mask method above)
                -----imageContainer (the place where the dynamically loaded imge gets loaded into)

                on loadInit() of the image loading, I center the parent clip, and off-set the imageContainer (as well as the lineContainer and maskContainer clips) to that when I scale the 'parent' clip.. the imageContainer clip scales form the center..

                however when I invoke the method above.. the lineContainer clip that I use as the source inverseMask is placed correctly.. (I checked by not making it invisible in the inverseMask method above).. but the inversedMask is created else where on the image??

                thoughts?

                update:
                could it be this line;
                _BMP.threshold(_BMP, new Rectangle(0, 0, Stage.width, Stage.height), new Point(0, 0), "<", 0xFFFFFFFF, 0x00FF0000);


                since Im off-setting the imageContainer, the lineContainer (which is the clip that the inversemask(0 function is taking its source shape from to inverse)..?? do I need to change the sourceRect? or the destPoint? or something?
                YIKES!.. its killing me.

                Thanks
                • 6. Re: inverse mask script AS3 &gt; AS2
                  kglad Adobe Community Professional & MVP
                  there are several restrictions on using masks in flash including each movieclip can have, at most, one mask and one movieclip can mask, at most, one movieclip.

                  there are ways to work around the masking more than one movieclip by assigning each movieclip you want to mask, one parent movieclip.

                  when using the bitmapdata draw() method, you "draw" the object without any of the on-stage transformations (including alpha, position, rotation, scaling etc). to create a bitmap that reflects the on-stage transformations you need to use the other parameters (like matrix, color matrix) of the draw() method.
                  • 7. inverse mask script AS3 &gt; AS2
                    whispers Level 1
                    Hey thanks for the reply..

                    I guess I'll try to tackle the positioning (off-set) problem first... like I said I havent used the bitmap class much.. so Im not really sure how to go about fixing it using the matrix/color matrix options you outlined..

                    I was actually just playing with the threshold line and trying different parameters for the new Rect() and new Point)( params..but it gave me mixed results at best... (like only the lower & right side of the shape/mask I drew..)

                    so if I load an image into a clip..and move that clip before running the inversedmask() method above.. I need to look into tweaking the draw() line in the inversedMask()?

                    If I didnt mention before..this is AS2...

                    so do I editt his draw line/snippet of code:

                    _BMP.draw(mc, matrix, invert);

                    and add to parameters to it? or create anew one your saying?

                    sorry for being thick here..

                    thanks again for replaying.

                    update: isnt it already using the translate to match the position of the 'mc' (which should match the imageContainer in my project)

                    matrix.translate(mc._x, mc._y);
                    _BMP.draw(mc, matrix, invert);

                    when I do matrix.toString() it says tx-160..but the inversed mask looks to be 0/0 even though tracing says differnt?

                    (confused)

                    thanks

                    update#2:

                    for whatever reason. changing the translate to:
                    matrix.translate(0, 0);

                    seems to have worked.. thanks for letting me where to look..

                    I'll do more testing to see if just changing that has created any bugs!


                    now I just need to understand what you meant about the masking and assigning a parent thing...

                    can you maybe elaborate? or point me in the right direction again? remember Im using AS2
                    • 8. inverse mask script AS3 &gt; AS2
                      whispers Level 1
                      hmm..Im still having trouble searching for anything on your mask tip comment:
                      parent.parent clip (contentContainer)
                      ---parent clip (faceContainer)--
                      -----lineContainer (holes the shape/object Im using for my source of the inverse mask
                      -----maskContainer (empty clip to hold the dynamically created clip form the inverse mask method above)
                      -----imageContainer (the place where the dynamically loaded imge gets loaded into)


                      using the tweak inversedMask() function above... I create my inversedMask inside of the maskContainer clip...which does the inversed mask and masks the imageContainerClip...

                      all working fine...

                      but I want to add a mask to either the parentClip (faceContainer), or even its parent (the contentContainer) clip..

                      so obviously when I scale the face container it doesnt bleed out of the display area...

                      however when i apply a mask either manually or through AS to either of these clips, as soon as I load the image... draw my shape for the source of the inverse mask, and then actually click the button to execute the inversedMask() function.. the whole screen goes blank..

                      I remove the 'parent' masks and it works exactly as it should..

                      suggestions or things I can search for...


                      I made a new .fla and just tried to manually set some mask on a parent clip.. that holds seveal nested objects/clips/layers... one of those nested layers is actually a mask itself.. .. it seemed to work fine... so Im at a loss on how to fix this?

                      anybody?

                      On a side note:

                      gunforhire's AS2 post of his funtion I think had a typo...

                      clipToMask.mask = inverseMask(mask_mc);

                      should be
                      clipToMask.setMask = inverseMask(mask_mc);

                      I believe.. (just for anyone else finding this thread)
                      • 9. Re: inverse mask script AS3 &gt; AS2
                        kglad Adobe Community Professional & MVP
                        i'm sorry. i just don't have time to go through all your questions.

                        i know you should use the concatenatedMatrix to account all the on-stage transformations of your movieclip.
                        • 10. Re: inverse mask script AS3 &gt; AS2
                          whispers Level 1
                          the only question there was left, was to expand on your MASKING comment.. as it didnt make sense to me.

                          assign a parent or whatever it was you were saying.

                          there was nothing else...
                          • 11. Re: inverse mask script AS3 &gt; AS2
                            whispers Level 1
                            gunforhire-

                            (if your still around per chance)--

                            Im wondering if you have tried (and been successful) in using a parent mask in any way in conjunction with your method above?

                            whenever I try to add a mask to a parent clip..(that holds the inversedMask and the masked nested clips... the whole thing goes 'blank' (masked out).


                            any ideas?

                            thanks