Copy link to clipboard
Copied
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.
oh, i see. that's a compiler screw-up.
just cast "this" in your base class as a MovieClip:
public function set labelText(value:String):void {
MovieClip(this).textLabel.text = value
}
Copy link to clipboard
Copied
i don't see that problem: http://www.kglad.com/Files/forums/test.zip
Copy link to clipboard
Copied
That's because you didn't set up the code the way I explained. You put
public function set tfF(value:String):void {
this.tf.text = value
}
into the child classes. I'm saying Flash throws a compile-time error if you put that in the base class (Page in your sample), even if the child has that TextField
Copy link to clipboard
Copied
oh, i see. that's a compiler screw-up.
just cast "this" in your base class as a MovieClip:
public function set labelText(value:String):void {
MovieClip(this).textLabel.text = value
}
Copy link to clipboard
Copied
Wow, I never would have thought of that! That did the trick. Thank you very much!
Copy link to clipboard
Copied
you're welcome.
that's a common compiler short-comming though that's the first time i've seen it in that guise. a more typical problem triggering an 1119 error is with:
root.variable_defined_on_root; // root will need to be cast as a MovieClip
or
this.parent.variable_defined_on_a_parent_timeline; // this.parent often needs to be cast as a MovieClip