4 Replies Latest reply on Jun 30, 2008 12:29 PM by yelgiw

    Determining label name of button instance

    yelgiw
      Hi! I'm new to actionscript and learning a lot but I'm having trouble organizing my code.

      I have 12 button instances in my project, each with a distinct label. For each button I'm applying different sounds for different button actions, such as press, release, rollOver, ect. While the sound for each action category is different the sound for each button is different with in a given category. For example depending on what button you press the sound played is different. Right now I have four different actions I'm working with, which means I have 48 sound cues!

      While my actionscript code works, it's very unorganized and hard to follow. Basically I have my wav files in an array like this:

      var press_array:Array = new Array("press0.wav", "press1.wav", "press2.wav", "press3.wav", "press4.wav", "press5.wav", "press6.wav", "press7.wav", "press8.wav", "press9.wav", "press_astric.wav", "press_pound.wav");

      I have an wav file array for each action; press, release, rollOver, rollOut.

      Then for each button I have this set of code:

      var press1:Sound = new Sound();
      press1.attachSound(press_array[1], true);

      btn1.onPress = function() {
      press1.start();
      };

      I have a defined variable and a button interaction function for each action/button combo and my script is now over 300 lines, but most of it is redundant.

      Is there a way to call the label of a button? For instance when the mouse is in the hit area of btn1, then my_btn=btn1. Then I could have a much shorter script that looked something like:

      when mouse is in hit area of a button
      get(button_name)
      my_btn=button_name

      if
      my_btn=btn1
      int=1
      end

      var press:sound = new Sound();
      press.attachSound(press_array[int], true);

      my_btn.onPress = function(){
      press.start();
      }
      end

      As I said before, the code I have right now is working but it's just really messy and hard to debug. If anyone could help me this I would greatly appreciate it! I'm also open to any suggestions in general. Thanks!
        • 1. Re: Determining label name of button instance
          Level 7
          To return the instance name of the object use _name. To return the label of
          a button instance use label.

          Example:

          mybtn.onRelease = function(){
          trace("name: "+this._name)
          trace("label: "+this.label)
          if(this._name == "mybtn"){
          trace("true")
          }else{
          trace("false")
          }

          }


          hth


          --
          Dan Smith > adobe community expert
          Flash Helps > http://www.smithmediafusion.com/blog/?cat=11
          http://www.dsmith.tv


          • 2. Re: Determining label name of button instance
            Bob Pierce
            I've put 2 buttons on stage with labels Btn_1 and Btn_2. They have instance names btn1_btn and btn2_btn. The code below uses a for-in loop to identify all instances with the name *****btn and extracts the number from the label. It adds this number as a property of each button called .indx. It then creates an onRelease function for each button which traces the button instance name and the indx from the label. From this you can see that with a more sophisticated routine you can extract the indx from the instance name and put a meaningful name on the label. You can replace the trace statement with your actions.
            • 3. Re: Determining label name of button instance
              yelgiw Level 1
              Thanks so much for your help!
              • 4. Re: Determining label name of button instance
                yelgiw Level 1
                Hi Dan. I have another question to help me expand upon the code you gave me earlier. I think I'm having trouble separating an object name from a button label.

                When I use the code you gave me my output window displays this:
                name: mybtn
                label: undefined
                true

                When I click on the button in my workspace in the properties inspector I see that this button is an instance of "btn" and in the "instance name" text input box "mybtn" is shown.

                I think in my original post I was not using the proper terminology, because my button does not have a label. In my original post when I used the term "label" I was incorrectly talking about the instance name. How do I go about placing a label on button?

                Thanks for your help!