30Apr

Communicating To A Document Class File

No comments

Recently I have had an issue with a need for one of my class files to call a method within my Document Class file. I found a little tutorial online and wrote up a simple demo below:

Here is the Main.as Document Class File:

package
{
    import flash.display.Sprite;

    public class Main extends Sprite
    {
		private var mytext:Text;
		public var mystring:String = "Here is my text from the document class";

        public function Main()
		{
			var mytext:Text = new Text();
			addChild(mytext);

			mytext.callback();
        }

		public function populateText()
		{
			return mystring;
        }

    }
}

Here is another class file that communicates with the Main Document Class File:

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

    public class Text extends Sprite
    {
		private var tfield:TextField;

        public function Text()
		{
			// Text Formatting
			var tformat:TextFormat = new TextFormat();
			tformat.font = "Arial";
			tformat.color = 0xffffff;
            tformat.size = 30;

			// Text Field
        	tfield = new TextField();
			tfield.defaultTextFormat = tformat;
        	addChild(tfield);
        	tfield.width = 700;
            tfield.height = 100;
			tfield.text = "just a test";
        }

		public function callback():void
		{
			// Call the function in the Document Class File
			tfield.htmlText = "Text from Document Class File 'Main':\n" + Main(root).populateText();
		}

    }
}

Share/Save/Bookmark

Categories: Actionscript 3

Wednesday, April 30th, 2008 at 9:38 am and is filed under Actionscript 3. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply