3 Replies Latest reply on Nov 21, 2007 5:22 PM by Newsgroup_User

    onEnterFrame

    l_u_b_i_007
      Is "onEnterFrame" being taken as a listener? I guess everyting which is "on(Something)" is a listener isn't it? And also, is there another use for this action apart from a stage which has only one frame (i.e. onEnterFrame only invokes an action whenever the timeline hits the this particular frame so therefore there is no point placing onEnterFrame on 10th frame of a timeline because the code underneath would happen anyway..)

      ...sorry I was just having an argument with my mate.

      Thanks
        • 1. Re: onEnterFrame
          Level 7
          l_u_b_i_007,

          > Is "onEnterFrame" being taken as a listener?

          You didn't specify, but I'm going under the assumption you're talking
          about ActionScript 2.0. onEnterFrame is a movie clip event
          (MovieClip.onEnterFrame) that gets dispatched in relation to the movie's
          frame rate. If you write an event handler for it, you'll be "listening" to
          that event; if you don't, you won't. I'm not sure I understand what you
          mean by "is it taken?"

          > I guess everyting which is "on(Something)" is a
          > listener isn't it?

          I would call the Something in your on(Something) an event. The on()
          function in ActionScript is one way of handling events. The slightly more
          up-to-date onPress, onRelease, onEnterFrame, etc. are ultimately methods of
          their associated classes that are open-ended. They get fired when the event
          they represent suggests (on a mouse press, mouse release, frame enter,
          etc.), and if you associate a custom function for any of them, your custom
          function is executed when those events/methods are called.

          > And also, is there another use for this action apart
          > from a stage which has only one frame

          You can use this wherever you like, on any timeline. It's a MovieClip
          event, so any movie clip instance (main timeline or movie clip symbol) can
          have its onEnterFrame event associated with a custom function.

          > (i.e. onEnterFrame only > invokes an action whenever
          > the timeline hits the this particular frame so therefore
          > there is no point placing onEnterFrame on 10th frame
          > of a timeline because the code underneath would
          > happen anyway..)

          While I personally tend to use onEnterFrame handlers in single-frame
          movies, I wouldn't say that such a handler on frame 10 (or 20, etc.) would
          be useless. The playhead runs along the main timeline, say. It enters
          frame 10, where it encounters a number of this: a) a stop() function, b) a
          movie clip instance that wasn't in previous frames, and b) an onEnterFrame
          event handler that causes the new movie clip to dance around. In a case
          like that, it makes perfect sense to put the onEnterFrame handler on frame
          10, because it applies to a particular movie clip instance. Does that make
          sense?


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. onEnterFrame
            l_u_b_i_007 Level 1
            heh, thanks for your reply.

            I can't say anything but that I must agree with what was said.

            However, I didn't get the last bit -

            Say you have a timeline of 10 (20etc.) KEYframes and two layers - one for object (MC) and the other for Actionscript (you would be placing the ActionScript on the timeline rather than on to an object).

            So in layer 1 we have only an object with an instance name of "my_mc" right.
            Layer 2 consists of 9 empty keyframes where on the last one is an action.

            So now tell me, what is the difference between writing on the last frame:

            my_mc.gotoAndStop(2);

            AND:

            this.onEnterFrame = function(){
            my_mc.gotoAndStop(2);
            }

            .....?
            Considering the "my_mc" has a stop() event handler in the first frame I guess the only way how to achieve a difference would be:

            my_mc.onEnterFrame = function(){
            this.gotoAndPlay(2)
            }

            so it loops the thing, is that what you thought?

            Otherwise, both of these work the same way and I can't imagine where else I would use onEnterFrame event handler unless if it was -
            1. attaching AS to an object
            2. 1frame timeline.
            3. writing as mentioned above:

            my_mc.onEnterFrame = function(){
            this.gotoAndPlay(2)
            }

            into the first or any other frame of any long timeline so the event gets looped...


            My last question would then be - is it better for lower CPU demands etc. to use a listener with a particular event in it's declaration already or rather onEnterFrame...? Sorry it's hard for me to explain, let's have a look at an example of this code:

            this.onEnterFrame = function(){
            if(my_mc._"EVENT"){
            dothis;
            }

            OR:


            my_mcListener.on"EVENT" = function() {
            dothis;

            };
            my_mc.addListener(stageListener);


            I guess the second one is better since we are getting rid of one more loop therefore the process should run faster, right?
            However, I would still use rather the onEnterFrame handler for a repeating loop within one frame or whatsoever.

            thanks for your time.

            • 3. Re: onEnterFrame
              Level 7
              l_u_b_i_007,

              > Say you have a timeline of 10 (20etc.) KEYframes and two layers - one
              > for object (MC) and the other for Actionscript (you would be placing the
              > ActionScript on the timeline rather than on to an object).

              With you so far. :)

              > So in layer 1 we have only an object with an instance name of "my_mc"
              > right. Layer 2 consists of 9 empty keyframes where on the last one is
              > an action.

              Okay.

              > So now tell me, what is the difference between writing on the last frame:
              >
              > my_mc.gotoAndStop(2);
              >
              > OR:
              >
              > this.onEnterFrame = function(){
              > my_mc.gotoAndStop(2);
              > }

              Very big difference. In the first case, you're simply calling the movie
              clip by name -- by its instance name, my_mc -- and invoking the
              MovieClip.gotoAndStop() method on it. You're telling that one MovieClip
              instance, out of possibly many others, to stop on frame 2 of its own
              timeline.

              In the second case, you're assigning a function literal to the
              MovieClip.onEnterFrame event of the main timeline (because "this" refers to
              the main timeline [also a movie clip] in this context). The function
              literal does the same thing the first case does -- it tells my_mc to stop at
              frame 2 of its own timeline -- but it gives my_mc this instruction
              repeatedly. By default, the movie framerate is 12fps, which means you're
              telling my_mc what to do approximmately once every 83 milliseconds! In only
              one second, you've told my_mc *twelve times* to stop at frame 2. You really
              only need to tell my_mc that once. ;)

              > Considering the "my_mc" has a stop() event handler
              > in the first frame.

              Careful ... the MovieClip.stop() action is a method. There are
              basically three categories of class members. Properties are charateristics
              of an object (MovieClip._x, _y, _width, _height, _currentframe, etc.).
              Methods are things the object can do (MovieClip.play(), stop(),
              gotoAndPlay(), etc.). Events are things the object can react to
              (MovieClip.onRelease, onPress, onMouseMove, etc.).

              Using stop() or gotoAndStop() is not something you'd put into an
              onEnterFrame event handler. But, for example, you might do something like
              this:

              // on frame 10 ...
              my_mc.stop();
              my_mc.onEnterFrame = function():Void {
              if (this._y < Stage.height) {
              this._y++;
              } else {
              delete this.onEnterFrame;
              }
              }

              ... which would a) stop my_mc's timeline and b) cause it to physically move
              down the screen as long as its _y property is less than the Stage.height
              property, after which it would remove the associated function from its own
              event handler.


              David Stiller
              Contributor, How to Cheat in Flash CS3
              http://tinyurl.com/2cp6na
              "Luck is the residue of good design."