8 Replies Latest reply on Jun 26, 2006 12:19 PM by Peter Lorent

    Problem with EventListener

    ricklecoat Level 1
      As I've stated in all my recent posts to this forum, I'm a total newbie at AS and need all the help I can get.

      I've got a button (one of several, but I'll concentrate on one for now) and I want to use a Listener in order to have the button click carry out commands. On the advice of another helpful forum user I applied the code (see bottom of message) to the first frame of the movie; I've given it a simple GetURL command as I'm less likely to get that wrong. However, the button does nothing.

      (The button instance itself is wrapped inside a movie clip and the MC instance is what appear on the stage; this is so that I can target and change dynamic text, giving each button a different label at runtime.)

      If someone can tell me what I'm doing wrong with my Listener code I'd be really grateful; the project is stalled until I resolve this and I'm stumped. I'm sure it's something simple.

      Many thanks in advance;
      Rick

      Here's the code:

        • 1. Re: Problem with EventListener
          Peter Lorent Level 2
          >>(The button instance itself is wrapped inside a movie clip

          Then it's a path problem:
          var prodListener:Object = new Object();
          prodListener.click = function(evtObj) {
          getURL(" http://www.sharkattack.co.uk/");
          };
          button_mc.products_btn.addEventListener("click", prodListener);

          where button_mc is the instance name of your mc that contains the button.
          • 2. Re: Problem with EventListener
            ricklecoat Level 1
            Thanks for your reply Luigi.

            I thought it was a path problem too, and your response made perfect sense - -target the button inside the MC rather than the MC itself. But when I did that it still didn't work. So I decided to go the Occams Razor route and cut away all the extraneous items -- started with a fresh .fla. This new movie has only two frames, and one button -- a simple button this time, with no nesting of any sort.

            Frame one contains a stop command and the listener code for the button; the listener code tells the _root timeline to go to frame 2 and stop. Frame 2 contains a cloud shape, so I'll know easily if the button has worked or not.

            The button instance is called clicker_btn.
            The listener is called myListener.
            The button click still does not work, and I'm really pulling my hair out now.

            Here's the code that sits in Frame 1 of the new test file:
            Thanks.


            // Pause frame 1 until button clicked
            stop();

            // LISTENER
            var myListener:Object = new Object();
            myListener.click = function(evtObj) {
            gotoAndStop(2);
            };
            clicker_btn.addEventListener("click", myListener);
            • 3. Re: Problem with EventListener
              Peter Lorent Level 2
              That code should work. Post a link to your fla.
              • 4. Re: Problem with EventListener
                ricklecoat Level 1
                Hi Luigi.

                I know this is going to turn out to be either something incredibly obvious and stupid, or something incredibly obscure. But, as you say, the code looks like it should work. I even tried deleting the ASO files from the Control menu incase they were corrupt or something.

                Anyway, here's the super-simple version I did:
                FLA file

                Thanks for your help.
                Rick
                • 5. Re: Problem with EventListener
                  Peter Lorent Level 2
                  The button component supports event listeners. The regular Button Class doesn't...
                  • 6. Re: Problem with EventListener
                    ricklecoat Level 1
                    Oh, nuts. I'm not really up to speed with components yet; certainly I don't know how to visually customise them to look different from their standard, just-dragged-out-of-the-toolbox appearance.
                    Originally I just had standard button.onRelease commands in place for the buttons, but for some reason I found that this interfered with them displaying correctly -- they no longer showed their OVER states (I had not defined a separate DOWN state so I don't know whether that was affected as well, but I would expect it to be so).

                    So I'm not sure where to go from here.
                    Any ideas? Or, so you have any similar experience with frame-placed code suppressing the OVER states of buttons? (I should point out that the buttons, although not displaying correctly on a mouse-over, did work correctly when clicked).

                    Hmm. I've got an explanation now, but without knowing a lot more about components than I do, I'm still a bit stuck. Which is my fault I guess for not know more than I do.

                    Suggestions would be welcome.
                    Thanks for your help thus far, Luigi.

                    Rick
                    • 7. Re: Problem with EventListener
                      Andreq85 Level 1
                      sometime, when you have a button in a movieClip and you ad a onRelease to the movieClip it break all the over state of the button. but normaly, if you do this :

                      your_btn.onRelease = function() {
                      //do something!
                      }
                      you should keep your button state.
                      • 8. Re: Problem with EventListener
                        Peter Lorent Level 2
                        >>So I'm not sure where to go from here.
                        Well, I would hit F1 and search for button in the Tutorials & Samples. You'll find all the basics there.