7 Replies Latest reply on Jun 7, 2007 8:34 PM by apptaro

    Custom MovieClip class and loadMovie

    apptaro
      Hi, I am trying to create a custom MovieClip class with loadMovie, but it doesn't work.

      I created a movie clip symbol and action script linked to it, such as:
      class CustomMovieClip extends MovieClip { ... custom functions to define the behavior, onPress, onMouseMove, etc ... }
      It works all fine until I call loadMovie on an instance, trying to change the shape.

      As I searched, I figured loadMovie can mess you up if you do somthing like mc.onPress = .... but
      is this the case for a custom class? Hope not.

      Any help???
        • 1. Re: Custom MovieClip class and loadMovie
          kglad Adobe Community Professional & MVP
          are you using loadMovie() in your custom class? if so, what's the target and where's the target created? hint: it shouldn't be created in the scope of your custom class.
          • 2. Re: Custom MovieClip class and loadMovie
            apptaro Level 1
            I'm doing something like this:
            var customMovieClipInstance = some_mc.createChildAtDepth("CustomMovieSymbol", DepthManager.kTop);
            ...
            customMovieClipInstance.loadMovie("xxx.swf"); // change graphics

            I know loadMovie wipes out any attached event handlers, but does it wipe out event handlers defined in subclass??
            • 3. Re: Custom MovieClip class and loadMovie
              kglad Adobe Community Professional & MVP
              is some_mc a member of your class and createChildAtDepth() is a class method?

              if so, is some_mc a child of the main timeline? if not, flash won't find customMovieClipInstance. if you think it is, show the code used to create that instance.
              • 4. Re: Custom MovieClip class and loadMovie
                apptaro Level 1
                some_mc is not a member of CustomMovieClip class.
                createChildAtDepth is a built-in DepthManager class method, which is like MovieClip.attachMovie.

                I'll prepare complete NON-working example and paste it here soon.
                That would be much easier to look into the situation.
                • 5. Re: Custom MovieClip class and loadMovie
                  apptaro Level 1
                  1. Create a project MCTest.flp
                  2. Prepare Movie1.swf (a simple shape)
                  3. Create a Flash document MCTest.fla
                  4. Create a ActionScript file CustomMovieClip.as:
                  class CustomMovieClip extends MovieClip {
                  function onPress() {
                  trace("onPress");
                  }
                  }
                  5. Create a symbol CustomMovieClip:
                  Behavior: Movie Clip
                  Identifier: CustomMovieClip
                  AS 2.0 Class: CustomMovieClip
                  Export for ActionScript: Yes
                  Export in first frame: Yes
                  Draw a simple rectangle.
                  6. Edit ActionScript in MCTest.fla>Scene 1>Layer 1>1st Frame
                  attachMovie("CustomMovieClip", "customMovieClip1", 0, {_x:100, _y:100});
                  attachMovie("CustomMovieClip", "customMovieClip2", 1, {_x:300, _y:100});
                  customMovieClip2.loadMovie("Movie1.swf");
                  7. Preview and you see two movie clips.
                  If you click left, "onPress" is printed in the output.
                  If you click right, nothing happens.

                  What I want to do is to change graphics of CustomMovieClip,
                  but above doesn't work. Is there a proper way?
                  • 6. Re: Custom MovieClip class and loadMovie
                    kglad Adobe Community Professional & MVP
                    maybe someone else can help.
                    • 7. Re: Custom MovieClip class and loadMovie
                      apptaro Level 1
                      Solved the issue myself. I write it here so other people can find it.
                      Basically, you can't do loadMovie directly to CustomMovieClip class instance.
                      It will mess up all the methods you have defined in the class.
                      Workaround is to do loadMovie to a variable in the class.
                      Complete working example is here:

                      1. Create a project MCTest.flp
                      2. Prepare Movie1.swf (a simple shape)
                      3. Create a Flash document MCTest.fla
                      4. Create a ActionScript file CustomMovieClip.as:
                      class CustomMovieClip extends MovieClip {
                      function onPress() {
                      trace("onPress");
                      }
                      }
                      6. Create a symbol CustomMovieClipContent:
                      Behavior: Movie Clip
                      Draw nothing.
                      5. Create a symbol CustomMovieClip:
                      Behavior: Movie Clip
                      Identifier: CustomMovieClip
                      AS 2.0 Class: CustomMovieClip
                      Export for ActionScript: Yes
                      Export in first frame: Yes
                      Open editor and drag&drop CustomMovieClipContent, name it 'contents'
                      6. Edit ActionScript in MCTest.fla>Scene 1>Layer 1>1st Frame
                      attachMovie("CustomMovieClip", "customMovieClip1", 0, {_x:100, _y:100});
                      customMovieClip1.contents.loadMovie("Movie1.swf");
                      7. Preview and you see two movie clips.
                      Click on the object, and "onPress" is printed in the output.