28 Replies Latest reply on Apr 19, 2009 1:27 PM by kglad

    Referencing buttons

    Desprez10

      I have a large number of movieclip buttons. When the user clicks a button, I want to reference an array entry based on which button is clicked, and then do some stuff, also based on that number.

       

      So, if button 2 is clicked, data from array[2] will be used. If button 5 is clicked data from array[5] will be used, etc.

       

      The problem is that I can't tell what button is clicked. I initialy thought that I could name the instances by number, and then reference the button instance name to use in the array lookup. But naming the instances as a number causes problems, and trying to use 'this' doesn't seem to want to return the name anyway,

       

      So, what I've had to do is give each button a differerent 'on (release)' script that contains a variable value to access the correct array entry. This seems quite ineficient, especialy since I'll eventually have a couple hundred buttons.

       

      What I'm looking for is a script that only appears once, and knows what button was pushed, and then references the correct array entry. Also, this way, when I need to make changes to the script I only have to change it once, and not 200 times.

        • 1. Re: Referencing buttons
          kglad Adobe Community Professional & MVP

          you can either name your buttons conveniently (like btn1, btn2, etc) or you can assign a property  to each button (if you're using movieclip buttons) so each button knows its number:

           

          btn1.number=1;

          btn2.number=2;

          etc

           

          or

           

          for(var i=1;i<btnTotal;i++){

          this["btn"+i].number=i;

          }

           

          if you name your buttons conveniently you can use the _name property of your buttons and the flash string methods to extract the number associated with each button.

          • 2. Re: Referencing buttons
            Desprez10 Level 1

            I am naming them predictably, but the script inside the button isn't returning the name anyway, so I can't extract the number from the string.

            My buttons are named "h0" "h1" "h2" "h3" etc.

             

            I have a movieclip button named "h0" with this script:

            on (release) {

                trace(this._name);
            }

             

            doesn't return anything.

            Regardless, I would still have to put that script in every single button.

            I'm sure there is a better way to do this, but I'm not familiar enough with actionscript.

             

            Isn't there a way to put a script on that layer that can detect which button is clicked, and then return that instance name? That way one script will function for all the buttons, and get different results based on the button name returned.

            • 3. Re: Referencing buttons
              Desprez10 Level 1

              Actually, what might work better, is to create the buttons on the fly as the programs starts.

              Then they can be placed and named automaticaly (yay!)

               

              buttonnumber = h0._name.substr(1,3);

              trace(buttonnumber);

               

              works just fine for up to 3 digit numbers, so now it's just a matter of getting the script on that layer to know what was clicked, or possibly inserting script as the buttons are created.

              • 4. Re: Referencing buttons
                kglad Adobe Community Professional & MVP

                if that on(release) traces nothing, then you don't have a movieclip button.  you have a simple button.

                • 5. Re: Referencing buttons
                  Desprez10 Level 1

                  Well, it's a movie clip with the button option selected on the properties pannel. Apparently that's not a movieclip button.

                  So I changed it back to a movieclip, and the trace works, but now there's a different problem.

                   

                  movieclip instance "h110"

                  on (release) {
                      buttonnumber = this._name.substr(1,3)

                      trace(this._name.substr(1,3));
                      doStuff(buttonnumber);
                  }

                   

                  The trace returns 110, but now the function doStuff refuses to work.

                   

                  It doesn't seem to be a number/string problem either as the following ALSO won't run unless the movieclip is a button, and if it is a button, then 'this._name' doesn't work.

                   

                  movieclip instance "h110"

                  on (release) {
                       buttonnumber = 110;
                       doStuff(buttonnumber);
                  }

                  • 6. Re: Referencing buttons
                    kglad Adobe Community Professional & MVP

                    prefix your doStuff() function with _parent:

                     

                    _parent.doStuff();

                    • 7. Re: Referencing buttons
                      Desprez10 Level 1

                      Sheesh, this language is wierd.

                      Ok, now the function works, but that broke something ELSE.

                       

                      This is closer to the actual handler, notice the added paremeter in doStuff():

                       

                      movieclip instance "h110"

                      on (release) {
                          buttonnumber = this._name.substr(1,3)
                          _parent.doStuff(buttonnumber, weaponrange);
                      }

                       

                      where the variable weaponrange is currently getting its input from an Input Text field on the same layer.

                      This used to work, but now is somehow not getting the info.

                       

                       

                      EDIT:

                      Nevermind. I figured it out.

                       

                      movieclip instance "h110"

                      on (release) {
                          buttonnumber = this._name.substr(1,3)
                          _parent.doStuff(buttonnumber, _parent.weaponrange);
                      }

                      • 8. Re: Referencing buttons
                        Desprez10 Level 1

                        Now, this works, but I still have to put this script in every movieclip.

                        Is there a way to have a centralized script that detects button presses (and detects which button) without having to put script in the buttons themselves? Or will I have to script-generate the buttons and automaticaly include the scripts in them? Did that make sense?

                        • 9. Re: Referencing buttons
                          kglad Adobe Community Professional & MVP

                          attaching code to objects is undesirable for a number of reasons.  and you can centralize your coding for all buttons.  if they are conveniently named, they can be coded in a for-loop without listing them all in an array.  if they are not conveniently named, you can list them all in an array and code them in a for-loop.

                          • 10. Re: Referencing buttons
                            kglad Adobe Community Professional & MVP

                            for example, if you have buttons named h1,h2,...,h220 that all exist, you can code them all using:

                             

                            var tl:MovieClip=this;

                            for(var i=1;i<=220;i++){

                            tl["h"+i].ivar = i;

                            tl["h"+i].onRelease=function()

                            this._parent.doStuff(this.ivar, _parent.weaponrange); // if different buttons pass different 2nd parameters, change this line.

                            }

                            }

                            • 11. Re: Referencing buttons
                              Desprez10 Level 1

                              Awsome. You're the best.


                              I'll give it a shot and see what happens.

                              • 12. Re: Referencing buttons
                                Desprez10 Level 1

                                Ok, well, I get the idea of what the code is trying to do, but something isn't quite right.

                                 

                                1) The movieclips aren't clickable anymore.

                                 

                                2) Here is the trace results for the various variables:

                                 

                                the first 'this' gives '_level0'

                                 

                                tl["h"+i].ivar

                                tl["h"+i].onRelease

                                 

                                all give 'undefined'

                                 

                                3) Is there supposed to be a '{' after the 'tl["h"+i].onRelease = function()'  ?

                                 

                                I tried googling the first line, to maybe see some other examples of this technique and sus-out the problem, and wouldn't you know it - I got a lot of pages where you posted similar help to other people :/

                                Maybe I need reference a specific layer? Or add something to watch for mouse clicks? Dunno.

                                • 13. Re: Referencing buttons
                                  kglad Adobe Community Professional & MVP

                                  if you're getting undefined (in the for-loop where i IS defined), then your movieclips don't exist when that code executes (or your movieclips aren't named h1,h2,...).

                                   

                                  and yes, there should be a left-curly bracket after function() in the above code.

                                  • 14. Re: Referencing buttons
                                    Desprez10 Level 1

                                    The movieclips exist, and they are named correctly. Still undefined.

                                    I double checked and an retyped the script (sometimes copy/paste introduces invisible characters that cause errors), and compared the form to similar examples, and I can't see anything obviouls wrong. So...

                                     

                                    In case it matters, the script was placed on the same layer as the movieclips in frame 1 (the only frame of the project so far).

                                    The movieclips still remain unclickable, but I assume that will be fixed when tl gets the correct values.

                                    • 15. Re: Referencing buttons
                                      kglad Adobe Community Professional & MVP

                                      if you click on an empty part of the stage and then click on one of your movieclips do you see h1 or h2 or... in the properties panel?  if yes, change your fla's extension to swf and attach it to this forum.  if no, you  don't have instance names for your movieclips or they are misnamed.

                                      • 16. Re: Referencing buttons
                                        Desprez10 Level 1

                                        Ok, here it is.

                                        Note that I removed the code from the first three movieclip hexes, just in case it was somehow interfereing, and limited the for loop to encompas only those first three.

                                        The code we are troubleshooting is on the "Hexes" layer.

                                        • 17. Re: Referencing buttons
                                          kglad Adobe Community Professional & MVP

                                          no attachment.

                                          • 18. Re: Referencing buttons
                                            Desprez10 Level 1

                                            That's strange. I'll try again.

                                            • 19. Re: Referencing buttons
                                              kglad Adobe Community Professional & MVP

                                              holy *)(#$&*%, you're using actionscript 1.

                                              1 person found this helpful
                                              • 20. Re: Referencing buttons
                                                Desprez10 Level 1

                                                Oh? Haha. How do I change that?

                                                Do I need up update to a newer version of flash? I've had this forever, I guess I should have check for updates.

                                                 

                                                It looks like I can get Flash CS3 for about $60. It's not the latest and greatest, but that should be fine, yes?

                                                Update: Hmmm. That might be a scam site selling it so cheap.

                                                Uodate 2: Yeah, it is almost certianly selling unauthorized software. It is russian, and sells downloads only. Darn.

                                                • 21. Re: Referencing buttons
                                                  kglad Adobe Community Professional & MVP

                                                  click file/publish settings/flash tab and select actionscript 2.0

                                                  • 22. Re: Referencing buttons
                                                    Desprez10 Level 1

                                                    It's not an option listed.

                                                    I'm running Flash MX from 2002, so that might be the issue.

                                                    I've been searching for upgrades to MX to use ActionScript 2.0, but either I'm not searching properly, or such an upgrade doesn't exist. (According to Wikipedia, ActionScript was introduced in Flash MX 2004.)

                                                    Unless you know differently, It looks like I have to get an updated copy of Flash.

                                                    • 23. Re: Referencing buttons
                                                      kglad Adobe Community Professional & MVP

                                                      flash mx was from 2004 and supported flash player 6 with actionscript 2.0.

                                                       

                                                      when you click the flash tab and then click script, is the only option actionscript 1.0?

                                                      • 24. Re: Referencing buttons
                                                        Desprez10 Level 1

                                                        There is no "script" to click.

                                                        I've attached a screenshot of the Publish Settings window.

                                                         

                                                        FWIW, the About box for the software says (c)1993-2002, so this tends to agree with the wiki entry. Flash MX, and Flash MX 2004 seem to be different products, though I'm honestly suprised that there isn't an update for ActionScript 2.0 for the former.

                                                        • 25. Re: Referencing buttons
                                                          Desprez10 Level 1

                                                          Dang it, the attachment just sits at "QUEUED" and wouldn't attach, just like last time.

                                                           

                                                          EDIT: Hmmm. Attachment still won't attach.

                                                          Anyway, there's an option to change the version (up to flash 6) but no options for ActionScript version. And nothing in the preferences either.

                                                          • 26. Re: Referencing buttons
                                                            kglad Adobe Community Professional & MVP

                                                            can you open an as2 fla in your ide?

                                                             

                                                            i'm uploading one to www.kglad.com/Files/test.fla.

                                                            • 27. Re: Referencing buttons
                                                              Desprez10 Level 1

                                                              Nope.

                                                              It either crashes, or hangs the program.

                                                              • 28. Re: Referencing buttons
                                                                kglad Adobe Community Professional & MVP

                                                                then try clicking help/updates to see if you can update from within the ide.  if you cannot, i think you'll need to buy a newer flash ide to use actionscrpt 2.0

                                                                1 person found this helpful