Getting a reference to a textbox in order to dynamically change the font
I am unfamiliar with Flash and actionscript and I need help to dynamically change the font of a textbox based on the value of a flashvar. I have a flash template whose content I can edit through XML. The flashvar value specifies the language and points to a different XML file. This works without problems, and I have found online a way to change the font via actionscript. However I need help on how to get a reference to that textbox.
There are two files an .fla and Main.as file. Within th开发者_StackOverflow社区e fla file there is a single Scene and when the textbox is selected the properties display "thisLogo" as its name and "Dynamic Text" in the combo box below. Within the Main.as file I have attempted to add the following code:
//...
public class Main extends MovieClip {
private var _root = root as MovieClip;
private var _parent = parent as MovieClip;
private var xmlURL:String = 'xml/main.xml';
//...
public function Main() {
var myflashvars:Object = new Object();
if (!this.loaderInfo.parameters.language){
myflashvars = {language: ""};
} else{
myflashvars = this.loaderInfo.parameters;
}
var langvar:String = myflashvars['language'];
xmlURL = 'xml/main' + langvar + '.xml';
var myformat:TextFormat = new TextFormat();
myformat.font = "Arial";
//thisLogo.embedFonts = true;
//This is the part I do not know how to reference the textbox from the code.
_root.thisLogo.setTextFormat(myformat);
//I have also tried:
//this.thisLogo.setTextFormat(myformat);
//thisLogo.setTextFormat(myformat);
Obviously the error I receive is the following:
1119: Access of possibly undefined property thisLogo through a reference with static type Main.
First I would suggest adding an if
statement that tests whether the Main
object's stage property is not null before executing the rest of the code. If it is null(which is usually isn't) then you can add an event listener that listens for your Main
object to be added to the stage:
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
Next you get the reference to your textfield and store it in a local TextField
object by calling the stage
property's getChildByName()
method and parsing the name of your textfield(I'm assuming its "thisLogo"?). Then you convert the DisplayObject
object returned by the getChildByName()
method to TextField
.
var thisLogo:TextField = TextField(stage.getChildByName("thisLogo"));
Now try
thisLogo.setTextFormat(myformat);
Overall it should look like this:
package
{
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite
{
private var xmlURL:String = "xml/main.xml";
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var myflashvars:Object = new Object();
if (!this.loaderInfo.parameters.language)
{
myflashvars = { language: "" };
}
else
{
myflashvars = this.loaderInfo.parameters;
}// end else
var langvar:String = myflashvars['language'];
xmlURL = 'xml/main' + langvar + '.xml';
var myformat:TextFormat = new TextFormat();
myformat.font = "Arial";
var thisLogo:TextField = TextField(stage.getChildByName("thisLogo"));
thisLogo.embedFonts = true;
thisLogo.setTextFormat(myformat);
}// end function
}// end class
}// end package
精彩评论