# My first simple Document Class not working. Please help

**URL:** https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380
**Category:** Uncategorized
**Created:** [July 8, 2008, 9:43am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380 "2008-07-08T09:43:01Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)
#### Post date: [July 8, 2008, 9:43am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/1 "2008-07-08T09:43:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)
#### Post date: [July 8, 2008, 10:15am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/2 "2008-07-08T10:15:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)
#### Post date: [July 8, 2008, 10:18am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/3 "2008-07-08T10:18:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)
#### Post date: [July 9, 2008, 9:02am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/4 "2008-07-09T09:02:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [July 9, 2008, 9:23am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/5 "2008-07-09T09:23:42Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)
#### Post date: [July 9, 2008, 10:29am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/6 "2008-07-09T10:29:29Z")

</div>

> 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

```auto
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

```auto
textField_txt.text="This is my Class";

```

is replaced with

```auto
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

```auto
var myclass:MyClass = new MyClass(this)

```

and alter MyClass so that its constructor takes a parameter.

```auto
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?

---

<div class="post-metadata">

### Author: ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)
#### Post date: [July 9, 2008, 10:48am UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/7 "2008-07-09T10:48:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)
#### Post date: [July 9, 2008, 1:22pm UTC](https://forum.kirupa.com/t/my-first-simple-document-class-not-working-please-help/265380/8 "2008-07-09T13:22:07Z")

</div>

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 🙂
