5 Replies Latest reply: Jul 11, 2013 12:40 PM by kglad RSS

    Library MovieClip linkage and class inheritance

    xTLS Community Member

      I've come across this problem several times and never figured out a clean solution; might as well try now. I'll explain the problem with a simplified example.

       

      I have three different MovieClips in my library used as popup dialogs. They each share core functionality but have some different controls. For this reason it makes sense to use class extension. However, trying to have the base class refer to a control found in all three child classes causes an error.

       

      Say we have

       

      Dialog is a base class that extends MovieClip but is not directly linked to anything in the library

       

      MovieClip NotificationDialogMC is linked to the class NotificationDialog

           NotificationDialog extends the class Dialog

       

      MovieClip SaveDialogMC is linked to the class SaveDialog

           SaveDialog extends the class Dialog

       

      MovieClip OpenDialogMC is linked to the class OpenDialog

           OpenDialog extends the class Dialog

       

      NotificationDialogMC, SaveDialogMC, and OpenDialogMC all contain a TextField named "textLabel" that is supposed to behave in the same way. Normally if you link a library MovieClip to a class, you can refer to children inside the MovieClip directly by their instance name. So I try to create a function in the base class as follows:

       

       

      public function set labelText(value:String):void {

           this.textLabel.text = value

      }

       

      Although "textLabel" exists in all three children, during compilation Dialog gives the error "Access of possibly undefined property textLabel" because it does not exist in the base class. However, if I create a variable called textLabel, this causes a conflict because the base class and the MovieClip linked to the child class have two different objects with the same name.

       

       

      I can fix this problem by making Dialog a dynamic class, but making a class dynamic opens a whole host of other issues. Is there another strategy that will work here? I know I could add the TextField in code, but I feel like coding static layouts instead of creating them in the IDE is wasting one of the biggest benefits of Flash.

       

       

      The forum text editing pane is not rendering this post correctly at all; hopefully that gets fixed when I post it.