13 Replies Latest reply on May 6, 2006 1:11 PM by Peter Lorent

    Can't get onEnterFrame working on MC in a class

    scottPadgett Level 1
      Hi,

      Can anyone take a look at this class file that's in the works, and help me understand why the onEnterFrame that's created in the startRewind() method is not working?

      Much thanks!

        • 1. Re: Can't get onEnterFrame working on MC in a class
          Craigus86 Level 1
          this.rewinder_mc.onEnterFrame = function()

          Where are you calling the startRewind() function from?
          because you have "this" which would refer to the object you are calling from.

          for the code above to work you would need to have a mc on the stage and call the function from a layer therefore this would refer to _root or the stage. If you are calling it from another mc then it will be looking inside that mc for another mc called "rewinder_mc"
          • 2. Re: Can't get onEnterFrame working on MC in a class
            scottPadgett Level 1
            I'm instantiating an instance of this mp3Player class in a movie and then calling the startRewind() method on that instance by clicking on the rewind button. Like this:

            on frame 1:

            var mp3Player_inst:mp3Player;
            mp3Player_inst = new mp3Player("songList.xml");


            on Button:

            on(press)
            {
            mp3Player_inst.startRewind();
            }
            on(release)
            {
            mp3Player_inst.stopRewind();
            }

            So it should look for the rewinder_mc MovieClip that is created in the mp3Player instance by the constructor function, right?
            • 3. Re: Can't get onEnterFrame working on MC in a class
              pwyon Level 1
              I tried what you said in a more simple manner. I not the best actionscripter in the world, but this is what I came up with:

              ////The Class Code////
              class myClass extends MovieClip{

              var rewinder:MovieClip;

              function myClass( ){
              rewinder = this.createEmptyMovieClip("rewinder" , this.getNextHighestDepth( ) );
              }//end constructor


              public function startRewind( ){
              this.rewinder.onEnterFrame = function(){
              trace( "Rewinding" );
              }//end onEnterFrame
              }//end function

              }//end class

              -----------------------------------

              ////CODE ON MAIN TIMELINE////
              var mclass:myClass;
              mclass = new myClass( );

              this.attachMovie( "myClip" , "myClip" , 1 );// Has "myClass" attached to it.

              mclass.startRewind( );//doesn't work

              myClip.startRewind( );//does work

              Hope that helps!
              • 4. Can't get onEnterFrame working on MC in a class
                scottPadgett Level 1
                Hi pwyon,

                I see what you're doing there. I just read up on attaching a class to a MC in the Actionscript Dictionary.

                But what I want to do is have a programatically created instance of the mp3Player class. I want to be able to write my class so I can create these things on the fly or whatever, so I need to get the class to create the movieclip that the onEnterFrame runs off entirely on its own.

                Anybody know how to do so? I've seen an example that does roughly what I'm doing, but this just isn't working for me the way I have it written.

                • 5. Re: Can't get onEnterFrame working on MC in a class
                  kglad Adobe Community Professional & MVP
                  you want your class to create a member of your class? or you want to create a child movieclip of your class member and use an onEnterframe handler attached to the child?
                  • 6. Re: Can't get onEnterFrame working on MC in a class
                    Peter Lorent Level 2
                    Just think about what you are doing. You are extending the MovieClip Class thus establishing a one-to-one relationship between the movieclip on the stage and the class that is attached to it. The instance is instantiated once you put the movieclip on the stage. Which normally means you don't use a constructor. So, you have the clip on the stage (either by dragging an instance from the library on the stage or with attachMovie) and you access the methods in the class using the instance name of the movieclip. So, if you have the clip on the stage, give it an instance name and use
                    instancename.startRewind() and on the button _parent.instancename.startRewind().

                    But you use the constructor to init an instance and effectively you now have two instances to deal with. Unfortunately there is no relationship between the clip you have on the stage and the instance you created using the constructor.

                    Either extend the MovieClip Class and access the methods using the instance name of the clip or don't extend the MovieClip Class and put an instance of the clip on the stage using attachMovie. The latter involves correcting the class since you cannot point to 'this' anymore where you target the clip on the stage.
                    • 7. Re: Can't get onEnterFrame working on MC in a class
                      scottPadgett Level 1
                      I think maybe pwyon's reply has confused things a bit? Or maybe I'm mistaken. But I have no MovieClip on the stage at all. There is only the movie itself and controller buttons. In frame one an instance of the mp3Player is instantiated. Then when you click on a button (say rewind) it's appropriate method is called on the instance of mp3Player (startRewind).

                      mp3Player extends the MovieClip class so I can call the createEmptyMovieClip method within it's constructor function to create a movie clip to run the onEnterFrame method from, thus allowing the class to execute a function on every passing frame. i think it's creating this new movieclip as a child of the mp3Player instance right?

                      Does this clear things up? Am I trying to do something that is not possible? This example I've referenced never has to attach a movieclip at all in order to execute an onEnterFrame function on a clip that it creates.

                      Here is a zip of this whole thing in case anybody can take a second:

                      http://myraandscott.com/mp3PlayerFiles.zip

                      PS, you have to press stop to load the file before it will play, in case you want to do so. But pressing the rewind btn without playing will demonstrate the problem.

                      Thanks.
                      • 8. Can't get onEnterFrame working on MC in a class
                        scottPadgett Level 1
                        To answer your question explicitly, kglad, i think I want the latter scenario you described. To create a child MovieClip of the mp3Player instance (which should be able to have children since it extends MovieClip right?). And then define the onEnterFrame function for that child. Is that possible?

                        • 9. Re: Can't get onEnterFrame working on MC in a class
                          Peter Lorent Level 2
                          You only need to extend the MovieClip Class if you need all or most of the functionality of the MovieClip Class. In most cases it's better to use composition. Change your class as attached.

                          • 10. Re: Can't get onEnterFrame working on MC in a class
                            scottPadgett Level 1
                            OK, I think I get it now. You have to pass the class a target clip, because otherwise there's nothing to create a movieClip upon? Why can't you just create the instance of the class as a MovieClip and then call createEmptyMovieClip() on itself?

                            • 11. Re: Can't get onEnterFrame working on MC in a class
                              Peter Lorent Level 2
                              >>You have to pass the class a target clip
                              Not neccesarily. You could just as easily use:
                              rewinder_mc = _level0.createEmptyMovieClip("rewinder_mc",_level0.getNextHighestDepth());
                              But passing a target makes the class more flexible: you can pass any movieclip as a target.

                              >>because otherwise there's nothing to create a movieClip upon?
                              createEmptyMovieClip is a method of the MovieClip Class. So it needs an instance of a movieclip (which has the properties and methods of the MovieClip Class), otherwise the method is unavailable.

                              >>Why can't you just create the instance of the class as a MovieClip and then call createEmptyMovieClip() on itself?
                              That's the case when you extend the MovieClip Class and I've explained that (or at least tried to...) in my first reply. Extending the MovieClip Class requires two things: the custom class you write and a movieclip in your fla. With the 'linkage' option (Library) you assign the custom class to the movieclip (and you can't setup a Linkage identifier for the _root). It's a one-to-one relationship so you can't link other classes to the same movieclip. If that's the setup you want you need to put your button controls in a movieclip (other than the _root) and link the class to the movieclip. But, as I said, composition is in almost all cases a better setup. Only extend the MovieClip Class if you need all or most of the functionality of the MovieClip Class.
                              • 12. Re: Can't get onEnterFrame working on MC in a class
                                scottPadgett Level 1
                                Thanks for the further explanation LuigiL. Things are making more sense to me and will even more so, I think, as I start to read up more on the areas you're explaining. In the mean time this portion of the class I'm working on is now working. Thanks.