Ted
Ted

Reputation: 3885

How to simply load variables to my dynamic text using flashvars on Actionscript 3?

I set up a simple dynamic text on my stage. I want to use the flashvars approach:

var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;   //set the paramObj variable to the parameters property of the LoaderInfo object
for (keyStr in paramObj) // I only have one parameter to load
{
    valueStr = String(paramObj[keyStr]);
}

But what happens now, is:

How do I load this valueStr to my myText.text ?

It is not initialized yet...

Thanks!

Upvotes: 0

Views: 1647

Answers (3)

George Profenza
George Profenza

Reputation: 51847

Add an event listener for Event.COMPLETE on your loaderInfo object, so you know when your swf is fully loaded, and therefore all of it's properties are properly initialized/ready:

this.loaderInfo.addEventListener(Event.COMPLETE, ready);//wait for this swf to be loaded and have flashVars ready
function ready(event:Event):void{
    var params:Object = this.loaderInfo.parameters;
    var paramValues:String = '';
    for(var i:String in params) paramValues += i + " : " + params[i] + "\n";
    myText.text = paramValues;
}

Note that you can not have too much data in your flashVars, but you could pass an url to a text file that could hold more. In the example bellow if a flashVar named testFile points to a text file, that is then loaded and appended to myText.

var myText:TextField = addChild(new TextField()) as TextField;
myText.multiline = true;
myText.border = true;
myText.autoSize = TextFieldAutoSize.LEFT;
myText.wordWrap = true;

this.loaderInfo.addEventListener(Event.COMPLETE, ready);//wait for this swf to be loaded and have flashVars ready
function ready(event:Event):void{
    var params:Object = this.loaderInfo.parameters;
    var paramValues:String = '';
    for(var i:String in params) paramValues += i + " : " + params[i] + "\n";
    myText.text = paramValues;
    //load larger text from file
    if(params['testFile'] != null){
        var textLoader:URLLoader = new URLLoader(new URLRequest(params['testFile']));
        textLoader.dataFormat = URLLoaderDataFormat.TEXT;
        textLoader.addEventListener(Event.COMPLETE, textReady);
    }
}
function textReady(event:Event):void{
    myText.appendText(event.target.data);
}

Upvotes: 1

net.uk.sweet
net.uk.sweet

Reputation: 12431

From what you've said, it sounds like you're trying to do this on the timeline. Since you can access the loaderInfo object from any frame on the root timeline, you could simply add the following line in the actionscript panel on the frame in which your text field is instantiated:

// No need to loop through the parameters object as we're only interested in one
myText.text = LoaderInfo(this.stage.loaderInfo).parameters['myFlashVar']; 

Upvotes: 0

liupeixin
liupeixin

Reputation: 738

you can initialize and add textfield in the code.

var myTexts2:TextField = new TextField()
this.addChild(myTexts2);
var keyStr:String;
var valueStr:String;
......

Upvotes: 0

Related Questions