7 Replies Latest reply on Dec 31, 2006 12:24 AM by butcho

    actionscript button

    lreadl
      This seems like it should be simple, but I'm having a lot of trouble. I have a .as files with a class defined in it, and that class has a function defined in it. I also have a button attached to a movieclip attached to another movieclip on the stage. How do I get that function to be the onPress function of the button?
        • 1. Re: actionscript button
          lreadl Level 1
          Seemingly, all I should have to do is myButton.addListener(myClassInstance), and then in my class define

          function onPress() {
          myFunction():
          }

          but that doesn't seem to work. Is that right?
          • 2. Re: actionscript button
            fanglinyong Level 1
            i think the functin" onPress()" belonged to flash ,you must rename your function name ,!
            • 3. Re: actionscript button
              lreadl Level 1
              I'm pretty sure you can define the onPress event function for listeners.

              Forget what I wrote before, all I want to do is get a function I've defined in a class in a .as file to happen when I press a button on the stage. That really, really should be simple, right? I'm losing my mind here. Someone please help me.
              • 4. Re: actionscript button
                lreadl Level 1
                Nevermind, I got it. If anyone ever wants to know, you need this exact syntax:

                • 5. Re: actionscript button
                  lreadl Level 1
                  If you don't use this syntax, onPress won't know where crapTrace is, and it will be undefined. I know it has something to do with what "this" keyword refers to, but I'm not sure why my syntax words and not others.
                  • 6. actionscript button
                    Level 1
                    Well not that hard indeed!

                    First of all, i suggest that you change your function name, but that have nothing to do with your problem. So, you can simply add in the constructor of the class the code to trigger the onPress event. Here is an exemple :

                    class MyClass{
                    // constructor
                    function MyClass(){
                    _root.myButton.onPress = onPressEvent;
                    }

                    // onPress event function
                    public function onPressEvent():Void{
                    trace("onPressEvent from _root.myButton");
                    }
                    }

                    Maybe your gonna need to use the Delegate class if you have some scoping problem. Also, in that example, i used the _root keyword, but i suggest not to use it (especially in a class). I suggest to passed as a parameter to the class the path of the button (composition) or to use a dynamic reference to that button.

                    Hope that answer your question!

                    Happy New Year!

                    Butcho

                    // * EDIT : lreadl was faster than me
                    • 7. Re: actionscript button
                      Level 1
                      Your main prolem is a scoping problem. So here is how you can use the delegate class: