My first simple Document Class not working. Please help

Hello,

I am just learning to write a simple Document Class (MyClass.as). Here’s my files:

Flash File

  1. There is a TextField (textField_txt) on the stage.
  2. I also write a code in the flash file to find the Document Class

import com.example.MyClass;

Document Class

  1. I wish to display the text “This is my Class” on the TextField (textField_txt). However it does not work. Nothing happens, and no error. Here’s the code of the Document Class:

package com.example {
import flash.display.MovieClip;
import flash.text.*;

public class MyClass extends MovieClip {
	
	public function MyClass() {
		this.textField_txt.text="This is my Class";
	}
}

}

Could you please provide some guidelines to me?

Thanks and best regards

Alex

In your .fla file, have you specified MyClass as the Document Class?

Forgot to attach my own files.
Hope that helps.
Let us know if it doesn’t.

Hello,

Thanks for your help. Yes, it works.

Is it possible to use the “import com.example.MyClass;” and not to specify MyClass as the Document Class in the fla file.

I remember that I saw an example some time ago to use the “import.xxxxxx.CustomClass;” in the fla file and no need to specify the Class name in the Document Class.

Thanks and best regards

Alex

Do it the way DiamondDog suggested. You do not need to import your document class.

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?

MyClass is a child of the “boss”, so for it to be able to access a textfield in the fla you would either need to send it a parameter as you have done, or “parent.textField” should work too. The document class is replacing the root of your file, whereas importing external as classes is adding them as children. That’s the difference as i have recently discovered.

In AS3, there is a much greater decoupling between objects on the stage (DisplayObjects) and classes themselves. In AS2, it was pretty much a free-for-all with regards to accessing objects on the stage - in AS3, you need to separate out the two, start from the top where the display list does exist and then work down :slight_smile: