3 Replies Latest reply on Feb 11, 2007 2:23 PM by Peter Lorent

    Passing variables to functions

    allhailtheunderdog
      Hi,

      I'm relatively new to actionscript and have been chugging away at it for the last couple of weeks. I'm trying to call a function on a button press. This is easy enough, but when I then change this function so that it accepts arguments it all goes it a bit wrong. Even just adding a string as an argument and tracing this to the screen causes problems...

      Basically I want the function to be called with an arg when a button is pressed so something like:

      mybutton.onRelease = myfunction(1);

      myfunction(i:Number) Void
      {
      trace(i)
      if (i == 1)
      {
      do something;
      }
      else
      {
      do something else;
      }
      }

      What ends up happening is that when I test the movie the function is called straight away, and then fails to work when the button is pressed. If I get rid of the arguments then it works fine, only printing the number to the screen when the button is pressed. If I change the button press so that is calls the function without sending the argument, then 'undefined' gets written to the screen (but at least it picks up that the button has been pressed unlike when I pass an argument).

      All the tutorials I've looked at seem to have this as the standard way of passing arguments to a function so I'm a little bit in the dark as to what is going on. Any help would be greatly apprieciated.

      Cheers,

      Tom