8 Replies Latest reply on Sep 7, 2006 12:47 AM by Peter Lorent

    Inheriting from MovieClip

    ajitn
      Hi all,
      I'm new to Flash and I'm new to OOP, and so I'm having a bit of trouble writing ActionScript programs to do exactly what I want.

      I am trying to create a simple application, in which a MovieClip is dragged around the screen and a textbox gets updated with the MovieClip's coordinates somewhere else on the screen. I wanted to do this using custom classes, so I created a class called MovieClipCoordinates that would contain a MovieClip and a TextField. I am initializing the MovieClip as the output of a this.createEmptyMovieClip() of the main movie, and the TextField similarly.

      My intention is to have the TextField updated whenever the MovieClip is dragged. So, in the onRelease function of the MovieClip, I need to update the TextField of the MovieClipCoordinates object that the MovieClip is a property of.

      The problem is, I have no idea how to find out the MovieClipCoordinates that the MovieClip belongs to. I thought I would create a method in the MovieClipCoordinates class that would set the TextField, which would be called from the MovieClip.onRelease. So I figured I would not have a MovieClip inside the MovieClipCoordinates class, but instead another class inherited from MovieClip which also contains a reference to the MovieClipCoordinates class that it is a member of. However, the problem now is that I can no longer create this subclass through a call to createEmptyMovieClip. So how do I create it?

      I'm getting a bit confused at this point of time. Could someone please give me some idea of how to do this?

      Thanks.
      Ajit
        • 1. Re: Inheriting from MovieClip
          Peter Lorent Level 2
          Have a look at the attached class. Hope it helps.

          • 2. Re: Inheriting from MovieClip
            pls could you explain where do we have to put these codes? in Laye1 : Frame 1 actions?
            • 3. Re: Inheriting from MovieClip
              injpix Level 3
              Create a new AS(ActionScript) file named “MovieClipCoordinates.as” that resides with the FLA you are working on. Copy and paste all of the code EXCEPT the comment “//in the fla” and the lines of code below it in this new AS file.
              Now copy and paste that comment I mentioned and the lines below it in frame 1 of the FLA.
              • 4. Re: Inheriting from MovieClip
                ajitn Level 1
                LuigiL,
                Thank you very much for your assistance. I truly appreciate it.
                Though the code works perfectly, I wonder if I may trouble you to clarify a couple of questions that I have with your code. Looking particularly at this section of the code:


                private function setHandlers(target_mc:MovieClip):Void{
                //use a reference to the current object
                var thisObj:MovieClipCoordinates=this;
                target_mc.onPress=function():Void{
                this.startDrag();
                }
                target_mc.onRelease=function():Void{
                this.stopDrag();
                //get the new coordinates
                var thisX=thisObj.box_mc._x;
                var thisY=thisObj.box_mc._y;
                //and refresh the text
                thisObj.printCoordinates(thisX,thisY);
                }



                How is it legal to use thisObj inside the onRelease function? I was under the impression that the onRelease function is a separate function altogether, that is called in the context of the MovieClip that it is associated with. How does it have knowledge of thisObj, which was defined in scope above it?

                Would it be even possible to define the onPress and onRelease functions above as two separate functions (CoordinatesPress and CoordinatesRelease, for example) and rewrite the above code as follows?:

                private function setHandlers(target_mc:MovieClip):Void{
                //use a reference to the current object
                var thisObj:MovieClipCoordinates=this;
                target_mc.onPress=CoordinatesPress;
                target_mc.onRelease=CoordinatesRelease;

                defining the handler functions separately. If I chose to do this, how would I possibly pass the thisObj to the handlers?

                I seem to be missing something fundamental in ActionScript functions, or perhaps just in the event-handlers. I would appreciate it if you could help me clear this up.

                Thanks again,
                Ajit
                • 5. Re: Inheriting from MovieClip
                  Peter Lorent Level 2
                  >>How is it legal to use thisObj inside the onRelease function?
                  Inside call back handlers the class members go out of scope. Solution 1: use a local (local to the function) reference to the current object to access the class members. Solution 2: use the Delegate Class (see attached example).

                  >>Would it be even possible to define the onPress and onRelease functions above as two separate functions
                  Sure. Then I would use the Delegate Class - see attached example.

                  BTW. I changed the code to reflect the coordinates during the drag.

                  • 6. Re: Inheriting from MovieClip
                    ajitn Level 1
                    LuigiL,

                    Thanks once again for your response. It did help to clarify some things.

                    However, I am still a bit confused about event handlers in general. They seem to be quite unnecessarily restrictive, and I am not quite able to formulate my code in terms of these functions.

                    The biggest problem for me seems to be nesting these handlers. The way I would want to use these is, for example, an object that is contained in another object should be able to call a function in the container object *after* it has finished executing certain statements in its own event handler. The container object's function should optionally be able to call *its* container object's function in an event handler, and so on.

                    I come from a background of C programming, and I would have done this by using a function pointer in the class, which can be modified when an object is added as a member of another class. Since these function pointers are separate for every level of container class, it would be possible for me to call a function in each of the container classes in a nested fashion whenever an event occurs on the lowest level class.

                    I don't see a way of doing this in Flash. Perhaps I am not thinking in 'object-oriented' terms? If so, how would you go about doing something like this?

                    Thanks very much,
                    Ajit
                    • 7. Re: Inheriting from MovieClip
                      ajitn Level 1
                      In reference to my previous message, I was wondering if it would be possible ot use the AsBroadcaster class to do the job. The contained objects could broadcast an event that the container object could listen to, and this could be extended to any depth of container objects.
                      Is this a good way of doing it?
                      • 8. Re: Inheriting from MovieClip
                        Peter Lorent Level 2
                        Yes, but I would prefer using the EventDispatcher Class (you can send an event object). Or - if applicable - a Delegation Event Model design pattern.