10 Replies Latest reply on Jun 8, 2013 2:44 PM by kglad

    create dynamic button with AS2

    teeronline

      Hi, this is my code, my problem is how to turn this["mc"+i] to a clickable button, see the code near the end? it doesn't do it, in fact on the screen it's not even 'button-abled'. I did some research, couldn't find anything at all, please help.

       

      what the script does to load a series of arrayFlag[i] (ext jpeg files) into "mc"+i, and supposingly when you click these jpeg on screen would loadMovie to another mc, right now they re not behaving like buttons

       

       

      for (var i=0; i<=flagArray.length; i++) {

          arrayFlag[i]="country/thumb/"+flagArray[i];

          arrayBg[i]="country/"+flagArray[i];

          //--------flags-------

          mybutton = this.createEmptyMovieClip("mc"+i, i);

          this["mc"+i].loadMovie(arrayFlag[i]);

          this["mc"+i]._x=95+distance;

          this["mc"+i]._y=0;

          //---------Name----------

          var str:String = flagArray[i].split(".jpg").join("");

          trace (str);

          this.createTextField("my_txt"+i, 100+i, distance, 12, 100, 100);

          this["my_txt"+i].text = str;

          this["my_txt"+i].setTextFormat(format2);

          //--------Text-----------

          this.createTextField("my_title"+i,200+i, distance, 0, 150, 100);

          this["my_title"+i].text = "Country Name:";

          this["my_title"+i].setTextFormat(format1);

         

          //-----loadMovie---------

         

          this["mc"+i].onRelease = function () {    <<-- not working

              mc.loadMovie(arrayBg[i]);

          }

          distance+=193;

      trace(flagArray[i]);

      }

        • 1. Re: create dynamic button with AS2
          teeronline Level 1

          turns out there's no way to turn import jpeg become clickable buttons,

           

          so the alternative is create another clip of the size and place it over the jpeg, while set the alpha value to 0 so jpeg underneath can be seen.

           

          this is code:

           

          for (var i=0; i<=flagArray.length-1; i++) {

              var a=i;

              arrayFlag[i]="country/thumb/"+flagArray[i];

              arrayBg[i]="country/"+flagArray[i];

              //--------flags-------

              mybutton = this.createEmptyMovieClip("mc"+i, i);

              this["mc"+i].loadMovie(arrayFlag[i]);

              this["mc"+i]._x=95+distance;

              this["mc"+i]._y=0;

              //---------Name----------

              var str:String = flagArray[i].split(".jpg").join("");

              trace (str);

              this.createTextField("my_txt"+i, 100+i, distance, 12, 100, 100);

              this["my_txt"+i].text = str;

              this["my_txt"+i].setTextFormat(format2);

              //--------Text-----------

              this.createTextField("my_title"+i,200+i, distance, 0, 150, 100);

              this["my_title"+i].text = "Country Name:";

              this["my_title"+i].setTextFormat(format1);

             

                 

             

             

              //-----loadMovie--------- create alpha = 0 clip over top of import jpeg --

              attachMovie("invbtton", "cover"+i, 300+i);

              this["cover"+i]._alpha = 0;

              this["cover"+i]._x = 95+distance;

              this["cover"+i]._y = 0;

              this["cover"+i].cover_inNum = i;

             

              this["cover"+i].onRelease = function() {

                  var bb = this.cover_inNum;

                  mc.loadMovie(arrayBg[bb]);

              }

             

             

              distance+=193;

              //trace(this["cover"+i].bb);

              //trace(arrayBg[i]);

          }

          • 2. Re: create dynamic button with AS2
            kglad Adobe Community Professional & MVP

            if you're trying to load a bitmap and create a button from the loaded bitmap, you are incorrect when you say there's no way to do that.

             

            you just need to wait until loading is complete before assigning mouse handlers to the load-target movieclip or, even easier, create a child movieclip and use that as the load-target and apply the mouse handler to the parent:

             

            for (var i = 0; i <= flagArray.length; i++) {

                arrayFlag[i] = "country/thumb/" + flagArray[i];

                arrayBg[i] = "country/" + flagArray[i];

                //--------flags-------

                mybutton = this.createEmptyMovieClip("mc" + i, i);  // your code would be easier for you to read if you used mybutton instead of this["mc" + i]

                var loadTarget:MovieClip = mybutton.createEmptyMovieClip("loadTarget",0);

                loadTarget.loadMovie(arrayFlag[i]);

                this["mc" + i]._x = 95 + distance;

                this["mc" + i]._y = 0;

                //---------Name----------

                var str:String = flagArray[i].split(".jpg").join("");

                trace(str);

                this.createTextField("my_txt" + i,100 + i,distance,12,100,100);

                this["my_txt" + i].text = str;

                this["my_txt" + i].setTextFormat(format2);

                //--------Text-----------

                this.createTextField("my_title" + i,200 + i,distance,0,150,100);

                this["my_title" + i].text = "Country Name:";

                this["my_title" + i].setTextFormat(format1);

             

                //-----loadMovie---------

             

                this["mc" + i].onRelease = function() {

                    mc.loadMovie(arrayBg[i]);  // mc must already exist

                };

                distance += 193;

                trace(flagArray[i]);

            }

            • 3. Re: create dynamic button with AS2
              teeronline Level 1

              Hi, Kglad

               

              thank you for reply.

               

              I'll try your method, it seems more straight forward than mine, but I have some problem to have another movie to load this movie to the stage

               

              If I run this movie by itself everything works fine. buttons are generated, and when they click, mc will be loaded with jpeg from 'country' folder.

               

              But if I have another swf (also written in AS2), load this movie to stage. Then these buttons work  just once and then freeze itself.

               

              the texts are not visible too, that might has to do with this movie is loaded to a masked layer. If I use the bitmapdata.draw method, its fine. It's the freeze thats killing me.

               

              I'm now trying to see if your method could help.

              • 4. Re: create dynamic button with AS2
                teeronline Level 1

                Hi, kglad.

                 

                I just tried your code,

                 

                this["mc" + i].onRelease = function() {

                     trace (arrayBg[i]);    <<---------      arrayBg[i] becomes undefined inside function

                        mc.loadMovie(arrayBg[i]);

                    };

                • 5. Re: create dynamic button with AS2
                  teeronline Level 1

                  sigh, somehow I'm now feeling that my only option is to add all the jpegs one by one manually, and asign each instance name manually... and thats near 200 jpegs there..  thats a bye bye bedtime

                  • 6. Re: create dynamic button with AS2
                    kglad Adobe Community Professional & MVP

                    if you want to retrieve the value of i when a movieclip was created, you can apply the string methods to the movieclip's _name property or, even easier, just assign the value of i to a movieclip property:

                     

                    this["mc"+i].ivar=i;  // stores the value of i (when the movieclip was created) in the movieclip's ivar property

                    this["mc" + i].onRelease = function() {

                         trace (arrayBg[this.ivar]);

                    // or, arrayBg[this._name.substr(2)], but using ivar is easier and less prone to errors.

                            mc.loadMovie(arrayBg[i]);

                        };

                    1 person found this helpful
                    • 7. Re: create dynamic button with AS2
                      teeronline Level 1

                      thanks a lot for reply , i got it work now.

                       

                      it all work out now, but as everything does work, it comes a little issue ( not really little )

                       

                      the level, I used 200+i, 100+i, 400+i on different elements on stage, when there r more than 30 or so jpeg sent to stage, with all the texts and stuffs,  somehow it become unstablized and affect the other components on the main movie.

                       

                      I just realized when you have 1 element on 1 level and it stack up to 500+ or so it really isn't a good idea.

                       

                      but if i don't do 200+i, just 200, for example. the next jpeg stay where the last one was.

                       

                      is there way get around it?

                      • 8. Re: create dynamic button with AS2
                        kglad Adobe Community Professional & MVP

                        there are 2 ways to ensure there are no depth collisions:

                         

                        1. control your depths using a variable (eg, dep).  initialize dep and then increment everytime it's used:

                         

                        var dep:Number=0;

                         

                        whatever.attachMovie("id","name",dep++);

                        something.createEmptyMovieClip("name",dep++);

                        etc

                         

                        2.  let flash control your depths (fp 8+) and use getNextHighestDepth():

                         

                        whatever.attachMovie("id","name",whatever.getNextHighestDepth());

                        something.createEmptyMovieClip("name",something.getNextHighestDepth());

                        etc

                        1 person found this helpful
                        • 9. Re: create dynamic button with AS2
                          teeronline Level 1

                          I tried to fix all the scripts I had so far to use getNextHighestDepth().. took a while

                           

                          thanks a lot.

                          • 10. Re: create dynamic button with AS2
                            kglad Adobe Community Professional & MVP

                            you're welcome.