22 Replies Latest reply on Dec 26, 2008 9:13 AM by W_Bell

    Simple Button Listener not so simple

    W_Bell Level 1
      Here is code that works fine without the Listener:

      myButton_Zoom = function () {
      bt_zoombig_mc._visible = !bt_zoombig_mc._visible;
      };

      Now when I try to add a listener it will not listen. Here is the code with the Listener:

      listener = new Object();
      listener.myButton_Zoom = function(objEvt) {
      bt_zoombig_mc._visible = !bt_zoombig_mc._visible;
      };
      myList.addEventListener("myButton_Zoom",listener);


      Can anybody please tell me why?
        • 1. Re: Simple Button Listener not so simple
          kglad Adobe Community Professional & MVP
          your syntax is screwed up. try:


          listener = new Object();
          listener.click = function(objEvt) {
          bt_zoombig_mc._visible = !bt_zoombig_mc._visible;
          };
          myList.addEventListener("click",listener);
          • 2. Re: Simple Button Listener not so simple
            W_Bell Level 1
            I think you may have missed the function call as your syntax does not call the function: myButton_Zoom ();
            I call this function from the nested button so the path with your syntax would actually be:


            listener = new Object();
            listener.click = function(objEvt) {
            controls_LH.bt_zoombig_mc._visible = !controls_LH.bt_zoombig_mc._visible;
            };
            myList.addEventListener("click",listener);


            But there is no relation to the function: myButton_Zoom ();
            Is there something missing in the code above as it does not work or do I need to call the function syntax differntly from the button?

            The button contains:

            on (release) {
            _root.myButton_Zoom();
            // more stuff here
            }
            • 3. Re: Simple Button Listener not so simple
              kglad Adobe Community Professional & MVP
              what are you trying to do?
              • 4. Re: Simple Button Listener not so simple
                W_Bell Level 1
                I have a menu button called "bt_zoombig_mc" with the following code:
                on (release) {
                _root.myButton_Zoom();
                }

                The function called by this button is in the main timeline frame and toggles visibility .
                Currently this works fine. Note that the code turns visibility false on the button clicked
                and then back on by the down state version button using the same code.

                myButton_Zoom = function () {
                controls_LH.bt_zoombig_mc._visible = !controls_LH.bt_zoombig_mc._visible;
                };


                My objective was to use the listener method in the code.
                • 5. Re: Simple Button Listener not so simple
                  kglad Adobe Community Professional & MVP
                  hopefully there's a 2nd button somewhere to make this button visible again:

                  • 6. Re: Simple Button Listener not so simple
                    W_Bell Level 1
                    This is the same code as you attached above. It will not work for 2 reasons.
                    Will not toggle visibility and will not call a function.

                    Maybe think from scratch and make 2 buttons:
                    ButtonUp_mc and ButtonDn_mc
                    Place ButtonUp_mc on top of the other.
                    Put the same code on both

                    If you can achieve the visibility then we are half way there.

                    Next, make 2 simple functions in the root like:

                    goto_frame = function () {
                    nextFrame();
                    };

                    goBack_frame = function () {
                    prevFrame();
                    };



                    One function is called from either button (goes to next frame and then goes back)
                    The goes back function is on the bottom and executes with the toggle code after
                    the user has clicked the top button and made it _visible = false.

                    Now we need to call these functions when the user clicks either button so
                    this function has to appear in the toggle code with the Listener either embbeded
                    with the Listener code or, outside following the listener code.

                    In short, the button executes the _visible code, then calls the function.
                    • 7. Re: Simple Button Listener not so simple
                      kglad Adobe Community Professional & MVP
                      if you want the button to call a function, put the function call in the listener.
                      • 8. Simple Button Listener not so simple
                        W_Bell Level 1
                        That is what you said before and I tried your method of code and it does not work.
                        It will not call the function and it will also not exectute the _visible action.

                        You say attach code but when I put your code in both the button or in the frame, both methods do nothing.

                        Could you be more expliced in your answer? If you read the previous post, I explained in detail the task at hand.
                        The code I supplied works if you tried it. You are offering this solution without an example so possibly
                        you have a "complete" simple example you could show that works for either the _visible action or the function call.

                        Thank you for taking the time for this not so simple task.
                        • 10. Simple Button Listener not so simple
                          W_Bell Level 1
                          This is the same code you suggested originally and I have tried putting it in both the Frame and on bt_zoombig_mc itself but nothing happens. may be this code is correct but why would it not even execute anything.

                          Is there something missing here? Do I need more code eleswhere to make this work?
                          I even tried changing bt_zoombig_mc to a button from a mc and still no results.

                          I tried to simplify your Listener code and it will not execute any action.

                          Have you tried this code? Maybe you could try making a simple button goto a frame.
                          This has not worked for me with your above suggestion so I don't know if you have actually
                          proven this method or not. If you test it you we see no results.
                          • 11. Re: Simple Button Listener not so simple
                            kglad Adobe Community Professional & MVP
                            it's not the same code and if it doesn't work, you're either not using a button component or you're screwing something else up.
                            • 12. Simple Button Listener not so simple
                              Aleex4
                              Sorry!
                              • 13. Re: Simple Button Listener not so simple
                                kglad Adobe Community Professional & MVP
                                don't open multiple threads about the same topic and especially, do not jump into someone else's thread with an irrelevant topic.
                                • 14. Re: Simple Button Listener not so simple
                                  W_Bell Level 1
                                  Thank you kglad,
                                  I hope the user can use proper Forum Ethics.

                                  I now see it does work with a button component.
                                  I was using a movie clip button so that explains why it would not fire.
                                  Is there a method with a mc button rather than a component?

                                  Thank you for your diligence with this problem.
                                  • 15. Re: Simple Button Listener not so simple
                                    kglad Adobe Community Professional & MVP
                                    for a button or movieclip you can either attach code to the object (which is undesirable and you already know how to do) or you can attach code to a timeline keyframe:

                                    bt_zoombig_mc.onRelease = myButton_Zoom;

                                    // or if you need to execute multiple lines of code not in one function line myButton_Zoom():

                                    bt_zoombig_mc.onRelease = function(){
                                    // lines of code
                                    // call functions: myButton_Zoom();
                                    // etc
                                    }
                                    • 16. Re: Simple Button Listener not so simple
                                      W_Bell Level 1
                                      OK, this is making sense now.
                                      So are you saying that you would not use a Listener with a movieclip button?
                                      I guess you would not do this anytime with a MC button?

                                      Is there a reason why not as It was the method I was curious about as it seems
                                      everything is done with Listeners.

                                      Anyways, I appreciate your help with this and I have used both methods you discribed.
                                      I think the last sounds the best as you suggested so if you have any followup on the
                                      above questions, I will close this thread then.

                                      Thank you,

                                      W Bell.
                                      • 17. Re: Simple Button Listener not so simple
                                        Rothrock Level 5
                                        This is why the event model in AS3 is better than in AS2.

                                        What you really seem to be interest in is the event. In AS2 there are three different ways of adding event handlers.

                                        addEventListener
                                        addListener
                                        onSomeEventHere

                                        Knowing which to use during which event and for which class is the trick. If you "grew up" using the AS2 style the new AS3 style seems difficult because the old ways messed you up so totally.

                                        In AS3 there is only addEventListener for everything.
                                        • 18. Simple Button Listener not so simple
                                          W_Bell Level 1
                                          Well this is good information and I have researched it some more since.
                                          I want to thank you both for your input.

                                          I am still not sure if there would be a reason to use a Listener or which one and how,
                                          when using a MC Button.

                                          The components I am using are AS2 so I am hoping to move up the AS3 soon
                                          but in the mean time I am hoping to learn more about controlling AS2 events.
                                          I have a related post I am making on a similar topic if you can help
                                          that would be great!

                                          W Bell.
                                          • 19. Re: Simple Button Listener not so simple
                                            sleetherz
                                            Anyone, how to edit the Flash Player?
                                            The game is already done, but the buyer want my game being change the language to Indonesian?
                                            Anyone know?

                                            http://vcc-gratis.blogspot.com

                                            LEARN U TO GET $$$
                                            • 20. Re: Simple Button Listener not so simple
                                              Rothrock Level 5
                                              What is with all the people hijacking threads lately?

                                              sleetherz -- Start your own thread!
                                              • 21. Re: Simple Button Listener not so simple
                                                kglad Adobe Community Professional & MVP
                                                he's a spammer and his post is not really a question. it's an attempt to get people to click his link.

                                                ignore it and let this thread die.
                                                • 22. Re: Simple Button Listener not so simple
                                                  W_Bell Level 1
                                                  sleetherz
                                                  Don't ruin my thread, get your own.
                                                  You are rude.
                                                  Learn some ethics!
                                                  Do you want me to do this to you?