Is it possible to use the “import com.example.MyClass;” and not to specify MyClass as the Document Class in the fla file.
Good question, because it’s revealing something that I clearly don’t understand about Document Classes.
If you use MyClass as your Document Class, as I suggested, it works fine.
If you don’t specify MyClass as your Document Class then you need to create your own instance of MyClass. (Remember that an instance of your Document Class is automatically created when you run your .fla file. Without a Document Class being specified, you need to create that instance yourself.)
You could do that with
var myclass:MyClass = new MyClass()
probably on frame 1 of your .fla file.
But that doesn’t work. You get an ‘undefined property’ error when myclass tries setting the text property on textField_txt.
Even if you alter MyClass so that
textField_txt.text="This is my Class";
is replaced with
MovieClip(root).textField_txt.text="This is my Class";
it still doesn’t work. If you run Debug, you find that ‘root’ is null.
That confuses me. I can’t see why ‘root’ should be null.
The only way round it I can see is to pass the root timeline in as a parameter to MyClass.
Have this on frame 1 of your .fla file
var myclass:MyClass = new MyClass(this)
and alter MyClass so that its constructor takes a parameter.
package
{
import flash.display.MovieClip;
import flash.text.*;
public class MyClass extends MovieClip
{
private var host:MovieClip;
public function MyClass(boss:MovieClip)
{
host = boss;
host.textField_txt.text="This is my Class";
}
} // end class
} // end package
It works, but I don’t understand why that should be necessary.
Clearly - well, it’s clear to me, anyway! - setting MyClass as your Document Class is the way to go, but I’d be interested to know why this other way doesn’t work.
Maybe someone can explain why ‘root’ is null?