12 Replies Latest reply: Sep 24, 2012 7:12 AM by kglad RSS

    EventListener not working - AS2

    ThnderBlt Community Member

      I have been tearing my hair out on this one.

       

      I found some code on a FAQ site that seems like it should work, but I can't get it to function. I have put an .flv video into my scene, it plays fine, but then I need the program to move to the next scene.

       

      Here is the code:

       

      onClipEvent (enterFrame) {

           var listenerObject:Object = new Object();

           listenerObject.complete = function(eventObject:Object):Void {

                stopAllSounds();

                trace ("Event Fired");

                _root.gotoAndPlay("First Time User",1);

           }

           this.addEventListener("complete",listenerObject);

      }

       

      I added in the trace to see if the function was firing at all, and I actually get the message in my output 12 times!

       

      I'd really appreciate any help with this.

       

      TIA,

      Jason

        • 1. Re: EventListener not working - AS2
          kglad CommunityMVP

          is your flv playing in an flvplayback component?  if yes, what's the instance name of that component?

          • 2. Re: EventListener not working - AS2
            sinious Community Member

            onClipEvent(enterFrame) { ... } is going to run that code every single frame. So you're setting up a listener over and over and over again, which explains the duplication. First thing is to remove that onClipEvent(enterFrame){} and just use the code inside it.

             

            You're applying the listener to "this" so whatever clip you pasted that code on the timeline inside has to fire off a complete event. Chances are (as kglads pointing out) what you're applying that listener to isn't the video.

            • 3. Re: EventListener not working - AS2
              ThnderBlt Community Member

              Yes, the instance name is logoFLV.

               

              The scene where all this is taking place has only 1 frame in the timeline. There are 5 layers.

               

              I tried commenting out the onClipEvent and got a bunch of 'Statement must appear within on/onClipEvent handler'

              The code is in the Actions section for the instance.

              • 4. Re: EventListener not working - AS2
                kglad CommunityMVP

                remove your code.  click on an empty part of the stage or back stage to deselect all objects.  in the actions panel, in the first keyframe that contains logoFLV, paste:

                 

                 

                     var listenerObject:Object = new Object();

                     listenerObject.complete = function(eventObject:Object):Void {

                          stopAllSounds();

                          trace ("Event Fired");

                         // _root.gotoAndPlay("First Time User",1);

                _root.gotoAndPlay("First Time User");  // don't use scenes in navigation with as2.  use frame labels only.

                     }

                logoFLV.addEventListener("complete",listenerObject);

                • 5. Re: EventListener not working - AS2
                  ThnderBlt Community Member

                  I got the first one working with a pretty ugly redirect, but I have a second scene where the technique is not working.

                   

                  Workaround:

                   

                  onClipEvent (enterFrame) {

                  var listenerObject:Object = new Object();

                  listenerObject.complete = function(eventObject:Object):Void {

                  stopAllSounds();

                  _root.gotoAndPlay(2);

                  }

                  this.addEventListener("complete",listenerObject);

                   

                  }

                   

                  Code in frame 2 then points to the next scene, and it works.

                   

                   

                   

                  Trying to use this in a later scene actually pushes me all the way back to the "First Time User" scene. I assume that this is because _root refers to the first scene only. Is there any way to reference any scene in the program (perhaps in reference to the _root)?

                   

                  @kglad:

                  I tried the technique you posted (and about half a dozen variants) and did not get any navigation to happeen at the end of the video. It actually doesn't even fire the event for some reason.

                  The program has been constructed over the last 3 months using over 90 scenes, so getting rid of the scenes at this point  is not feasible.

                  • 6. Re: EventListener not working - AS2
                    kglad CommunityMVP

                    what do the following traces show:

                     

                         var listenerObject:Object = new Object();

                         listenerObject.complete = function(eventObject:Object):Void {

                              stopAllSounds();

                              trace ("Event Fired");

                             // _root.gotoAndPlay("First Time User",1);

                    _root.gotoAndPlay("First Time User");  // don't use scenes in navigation with as2.  use frame labels only.

                         }

                    logoFLV.addEventListener("complete",listenerObject);

                    trace(logoFLV);

                    • 7. Re: EventListener not working - AS2
                      ThnderBlt Community Member

                      If I move the code from the instance to the first frame, the "Event Fired" doesn't show up at all.

                       

                      It traces the instance as "_level0.logoFLV"

                       

                       

                      If I try tracing this in the instance, I get a ton of 'Undefined' messages, and then 'Event Fired'.

                      • 8. Re: EventListener not working - AS2
                        kglad CommunityMVP

                        all that code should be on the same frame.  in addition, the timeline should not move from that frame or, if it does, there should be no keyframe in the layer with logoFLV.

                        • 9. Re: EventListener not working - AS2
                          ThnderBlt Community Member

                          Yeah, everything is together. The main timeline has 2 frames right now. Frame 1 has all of the content. Frame 2 is just a redirect to the next scene.

                          I have tried redirecting to the next scene, redirecting to Frame 2, and setting frame labels for either option. Either the traces throw 'undefined' or I just get '_level0.mainFLV'. I do not get the 'Event Fired' trace at all.

                           

                          If I put the code in a clip event inside the instance, the only way I get any movement is if I repeat the code from the first video. However, the navigation seemingly directs me to Frame 2 of the first scene instead of the current scene (9th).

                           

                          onClipEvent (enterFrame) {

                          var listenerObject:Object = new Object();

                          listenerObject.complete = function(eventObject:Object):Void {

                          stopAllSounds();

                          _root.gotoAndPlay(2);

                          }

                          this.addEventListener("complete",listenerObject);

                          }

                           

                          I do have a button in the scene which can be used to navigate forward. Is there a way to activate the on (release) method of the button at the end of the video instead?

                          • 10. Re: EventListener not working - AS2
                            kglad CommunityMVP

                            again, delete the code you are using.  there are so many problems with that code, it is not salvageable.

                             

                            click on logoFLV and cut the contentPath string from the properties panel.

                             

                            attached to the frame that contains logoFLV, what trace output do you see when you copy and paste the following code.  change nothing except to assign the path/name for your flv and allow to run until your video ends.

                             

                            stop();

                             

                                 var listenerObject:Object = new Object();

                                 listenerObject.complete = function(eventObject:Object):Void {

                                      stopAllSounds();

                                      trace ("Event Fired");
                            clearInterval(traceI);

                                     // _root.gotoAndPlay("First Time User",1);

                            _root.gotoAndPlay("First Time User");  // don't use scenes in navigation with as2.  use frame labels only.

                                 }

                            logoFLV.addEventListener("complete",listenerObject);

                             

                            logoFLV.contentPath = "path/filename.flv";   // paste the contentPath string you cut from the properties panel.

                            var tl:MovieClip=this;

                            trace(logoFLV,this,this._currentframe);

                            clearInterval(traceI);

                            traceI=setInterval(traceF,3000);

                            function traceF():Void{

                            trace(tl._currentframe);

                            }

                            • 11. Re: EventListener not working - AS2
                              ThnderBlt Community Member

                              Alright, that works perfectly! Is it just by putting in the contentpath in the code instead of when you add the file that makes the difference? I don't get it, but it is awesome that it is working.

                               

                              Flash didn't like the multi-parameter trace statement 'trace(logoFLV,this,this._currentframe);' so I commented out to test and I got 1 several times and then 'Event Fired'. I used it on the second video, and I got 391 a bunch of times because that video was much longer and the event fired as well.

                               

                              The last thing I have to do is play videos dynamically after a button click. Will this technique work for that as well? Actually, I figured this out. I am all set!

                               

                              Thanks so much,

                               

                              Jason

                              • 12. Re: EventListener not working - AS2
                                kglad CommunityMVP

                                you're welcome.

                                 

                                (sorry, that format for trace is from as3.  replacing the commas by +" "+ will work for as2.