3 Replies Latest reply on Oct 23, 2006 10:48 AM by warrblade

    Alternative to onClipEvent(load) ?

    warrblade
      How do you avoid using onClipEvent(load) ?

      In the Actionscript 2.0 Best Practices article they say to avoid using on() and onClipEvent() handler functions, because it forces you to attach actions directly to a MovieClip. This works fine when replacing this:

      on (release) {
      // Do something.
      }

      with this:

      myMovieClip.onRelease = function() {
      // Do something.
      };

      But replacing onClipEvent(load) with myMovieClip.onLoad doesn't seem to work... Anyone know of a workaround?
        • 1. Re: Alternative to onClipEvent(load) ?
          Level 7
          warrblade,

          > How do you avoid using onClipEvent(load) ?

          Ahh, this is a good question.

          > In the http://www.adobe.com/devnet/flash/articles/as_bestpractices_04.html
          > article they say to avoid using on() and onClipEvent()
          > handler functions, because it forces you to attach actions
          > directly to a MovieClip.

          Yup, and I agree, it's a much better way to go. But as you've already
          discovered ...

          > But replacing onClipEvent(load) with myMovieClip.onLoad
          > doesn't seem to work... Anyone know of a workaround?

          This stumped me, too, when I first encountered the issue. I'm not
          exactly sure *why* this is -- I should probably dig through Colin Moock's
          "ActionScript for Flash MX, The Definitive Guide" again -- but for the
          MovieClip.onLoad event, you may only handle the event of a class that
          extends MovieClip. In other words, you can't just use the instance name of
          your MovieClip instance and assign a function to the onLoad event ... you
          have to create an ActionScript 2.0 class that extends MovieClip, handle your
          event in that class, then associate your class file to a Library symbol by
          way of the Linkage ID.

          Here's a free tutorial from Community MX on how to subclass MovieClip.

          http://www.communitymx.com/content/article.cfm?cid=A06B3C7D7B74030D


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Alternative to onClipEvent(load) ?
            Level 7
            > In other words, you can't just use the instance name of
            > your MovieClip instance and assign a function to the
            > onLoad event ... you have to create an ActionScript 2.0
            > class that extends MovieClip, ...

            P.S. Here's exactly what the documentation states:

            <quote>Invoked when the movie clip is instantiated and appears in the
            timeline. You must define a function that executes when the event handler is
            invoked. You can define the function on the timeline or in a class file that
            extends the MovieClip class or is linked to a symbol in the library.</quote>

            The last sentence, beginning with "you can," is a bit misleading, I
            think. Sure, you can define the function on the timeline, but the function
            will only work if the MovieClip instance is associated with a class file
            (i.e., the instance is a subclass of MovieClip.

            Continuing on ...

            <quote>You use this handler only with movie clips for which you have a
            symbol in the library that is associated with a class.</quote>

            Bingo. That's the key.

            <quote>If you want an event handler to be invoked when a specific movie clip
            loads, you must use onClipEvent(load) or the MovieClipLoader class instead
            of this handlerl;</quote>

            When a "specific movie clip loads" is not quite specific enough, I
            think. If I had my druthers, this sentence to explicitely state that it
            refers to non-subclassed movie clips.


            David Stiller
            Adobe Community Expert
            Dev blog, http://www.quip.net/blog/
            "Luck is the residue of good design."


            • 3. Re: Alternative to onClipEvent(load) ?
              warrblade Level 1
              That helps a lot, thanks David!