4 Replies Latest reply on Jul 3, 2013 7:49 AM by brsastre

    How to concatenate a "name.target" with string?

    brsastre

      Hello, how are you?

      I have this issue:

       

      I have a function -let's call this function "dad", that receives a parameter -let's call it "parameter". Inside dad function, I need to concatenate parameter with a string, to call a function outside function dad. Example:

       

      function dad (parameter) {

      parameter.target.onPress = trace (parameter.target  + "_animation")
      --> this returns "_level0.button1_animation" which is the same as "button1_animation" right?

       

      but when i use it to call a function outside it doesnt work:

       

      parameter.target.onPress = parameter.target + "_animation" ; --> doesn't work.

       

      parameter.target.onPress = [parameter.target + "_animation"] ; --> doesn't work.

       

      but this does work:

      parameter.target.onPress = button1_animation

       

      }


      function button1_animation {

      here i have a tween

      }

       

      All i wanna do, is concatenate parameter.target with a string, to call a function outside! is it possible ?

       

      thanks in advance

        • 1. Re: How to concatenate a "name.target" with string?
          Ned Murphy Adobe Community Professional & MVP

          "...this returns "_level0.button1_animation" which is the same as "button1_animation" right? "

           

          wrong

           

          They are both string values at this point so they are not the same as each other. 

           

          What you need to do is strip off the unwanted portion and then use bracket notation to treat the string as a function.  When you use the bracket notation you need to precede it with a targeting reference such as "this" or some other relative reference, as in ...  this["button2_animation"]

          1 person found this helpful
          • 2. Re: How to concatenate a "name.target" with string?
            brsastre Level 1

            Ned, Thanks.

             

            I dont want to treat the string as a function, because I already have the function "button1_animation" outside. I need to call that function  when i do parameter.target.onPress where parameter is button1, button2, button3 (i am passing to the parameter an array of buttons, and i need each button to call it's own function (button1_animation, button2_animation, an so on...) this functions are outside the function  that receives "parameter".

             

            I finally could do it this way:

             

            parameter.target.onPress = eval[parameter.target_name + "_animation"]

             

            i really appreciate the time you took on helping me. Thank you very much.

            • 3. Re: How to concatenate a "name.target" with string?
              Ned Murphy Adobe Community Professional & MVP

              You are treating the string as a function call so I don't know why you say you don't want to.  Maybe our differences are just a matter of semantics

               

              By choosing the taregt_name instead of the target you are getting the string of the name without the _level1 appended to it, which is a quicker alternative to trying to extract it from the target data alone.

               

              Chances are you could have also used.... this[parameter.target_name + "_animation"]   ...if 'this' is in the proper frame of referrence

              1 person found this helpful
              • 4. Re: How to concatenate a "name.target" with string?
                brsastre Level 1

                Ned: "What you need to do is strip off the unwanted portion and then use bracket notation to treat the string as a function."

                Ned: "You are treating the string as a function call so I don't know why you say you don't want to. "

                 

                To me a function, and a function call are different. But i am begining with as2 (also programming) , sorry if i am not clear Ned.

                 

                Appreciate your help.