5 Replies Latest reply on Jul 25, 2009 2:33 PM by jshrek

    Can't get setMask to work at all

    jshrek Level 1

      I am using Flash CS3 with ActionScript 2.0


      I am trying to load a jpg in and display it in an existing movieClip.


      I have created a movieClip on the stage with an instance name of maskMovieClip. I then create a new/empty clip within maskMovieClip, and then load the new clip with the jpg and then try to use the setMask to switch them. I did read that it won't work if jpg's have not finished loading, so I put a for/next loop in my example just to make sure the clips are loaded. Here is my test code:



      //Create one movie clip on the stage with an instance name of maskMovieClip.

      //Create two small jpg pics called pic1.jpg and pic2.jpg.

      //This correctly loads and displays a jpg into maskMovieClip

      maskMovieClip.loadMovie("pic1.jpg", 1);


      //This supposedly creates a new movie clip called newMC within the maskMovieClip

      maskMovieClip.createEmptyMovieClip("newMC", maskMovieClip.getNextHighestDepth());


      //This loads a different jpg into newMC. And just to make sure it worked,

      //I manually tried creating a movie clip on the stage with the instance

      //name of newMC, and it correctly loaded and displayed it.

      newMC.loadMovie("pic2.jpg", 1);


      //This is just to kill some time to make sure both jpg's are loaded

      for (i=0; i<=100000; i++) {

      //This is supposed to switch from one to the other, but doesn't


      //I even tried this to make sure I didn't have it backwards



      Any assistance would be greatly appreciated!!!



        • 1. Re: Can't get setMask to work at all
          webqaflash Level 3

          To load the second picture


          You should call loadMovie like this.


          maskMovieClip.newMC.loadMovie("pic2.jpg", 1);

          1 person found this helpful
          • 2. Re: Can't get setMask to work at all
            Rothrock Level 5

            You are on the right track. You do have to wait until the external file loads before setting mask. But using a for loop...well, that isn't going to work.


            The loop probably executes in a few milliseconds. So unless your file is very, very small, or your internet is super, super fast...well even then it won't work.


            What you really need is to use the MovieClipLoader class. It allows you to have a lovely onLoadInit event which can then set the mask. Check it out in the help files and post back if you need help in using it.


            You are also making your newMC clip inside the clip you are planning to mask. Strange, but it can be done. But you will need to use the proper path:




            Also since jpgs don't support transparency or any shape other than a rectangle your mask is just going to be a rectangle. So I'm not quite clear why you need to load that file as a mask. You could just draw a rectangle.


            Finally I'm not sure I understand what you mean by "switch from one to the other." Generally if you are trying to switch back and forth between two rectangular movieclips you just change the _visible property of one of them. You only use masks if you are trying to do some kind of fancy transition or have part of something that you want to hide/show. So what are you actually trying to accomplish?

            1 person found this helpful
            • 3. Re: Can't get setMask to work at all
              jshrek Level 1

              Thanks webquaflash and Rothrock...


              I tried both of your suggestions and set corret paths by changing the following two lines to maskMovieClip.newMC.loadMovie("banner/pic2.jpg",1); and also maskMovieClip.setMask(maskMovieClip.newMC); but it still does not work.


              To answer Rothrocks questions...

              The program above is only an example to try and get setMask working. The for loop is just for testing purposes in the test program. The jpegs are quite small, and I can set the for loop to several seconds (like 500000) just for a quick test to give them time to load. I will be playing with the MovieClipLoader class later on, but I wanted to get this working first.


              I may going about this the wrong way, but what I am trying to accomplish is a slideshow type of program that will preload  several jpeg images each in their own MovieClip instance (hence the createemptymovieclip), and then I was going to use setMask to display each one every few seconds. I created the new MovieClip instance within the original maskMovieClip because maskMovieClip was manually created on the stage and it's layer is underneath another layer that has buttons that I want to appear on top of this layer. If I create and load new movieclips like this:

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

              then it loads properly, but it is on top of my buttons, and if I create and load multiple movieclip images, then they keep stacking up on top of the others. So I was trying to load them into the existing maskMovieClip so that their depth would stay the same and they would stay underneath the buttons.



              • 4. Re: Can't get setMask to work at all
                Rothrock Level 5

                You missed the point. There is no way ever that a for loop is going to give you the delay you need for loading the files. You can't do it that way.


                You must either use the MovieClipLoader class and the onLoadInit event or you must make an old-school preloader. There is no other way.


                This is a very strange use of setMask and really isn't what you need at all. If you are trying to only show portions of the loaded clips then it still isn't what you need.


                In that case make a clip called holder make another clip called holderMask inside the same parent.




                and then create your movieclips inside holder and load away. I think that should work.

                • 5. Re: Can't get setMask to work at all
                  jshrek Level 1

                  I gave up on using setMask... I could not get it to work even with MovieClipLoader class, so I took a different approach...


                  I load all the jpegs in like this into their own instance and assign them an increasing depth starting at 0. To display any particular jpeg just change it's _alpha back to 100:

                  for (j=0; j<total; j++) { //Pre-load all the images. total is the number of pics in the external XML file
                    picLoad = "newMC" + j; //Assign each image a unique name (newMC0, newMC1, etc)
                    this.createEmptyMovieClip(picLoad, j); //Set their depth equal to j (newMC0 is depth 0, newMC1 is depth 1, etc)
                    this[picLoad]._alpha = 0; //Set their alpha=0 so you can't see them
                    this[picLoad].loadMovie(image[j],1); //image[] is an array that contains the location & name of each pic (image[0] = "slideshow/pic1.jpg")



                  I then used swapDepths() to move the buttons from their original depth assigned by Flash (which is around -16000) to a higher depth starting at 50000, which is well above the depth of the jpegs I loaded in, so unless somebody wanted to load in more than 50000 images, it should not be a problem. There is actually 2 buttons, a text field and a movieClip (as a background for the buttons and text). I initially could not get swapDepths to work with the buttons and text field... it was only working with movie clips, but then I found out that swapDepths is not available to buttons and text fields by default, but needs to be included. All this was done like this:

                  Button.prototype.swapDepths = MovieClip.prototype.swapDepths; //Make swapDepths available for buttons
                  TextField.prototype.swapDepths = MovieClip.prototype.swapDepths;
                  //Make swapDepths available for text fields


                  Another possible alternative would have been to use swapDepths to swap each pic back and forth between a manually created movieClip on the stage, but it seemed easier to just write the code to swap my buttons once instead of code for swapping pics everytime!