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();
}
}
}
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.