Flashvars is undefined

1.4k views Asked by At

I am trying to pass parameters to my swf file using flash vars. I am using Swfobject to embed my swf. I, however, am not able to access these flashvars inside the swf file. I've been having trouble with this for weeks now. I have searched the internet, swfobject's documentation, and other questions related to this.

Here is my js code using the swfobject to embed the swf file.

<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
var flashvars = {};
flashvars.name1 = "hello";
swfobject.embedSWF("Comics.swf", "myContent", "950", "650","9.0.0","expressInstall.swf", flashvars, flashvars,flashvars);
</script>

Here is my the html code to be used for embedding

<div id="myContent"></div>

The swf is embedded fine and produces the following

<object type="application/x-shockwave-flash" name1="hello" data="Comics.swf" width="950" height="650" id="myContent" style="visibility: visible; ">
    <param name="name1" value="hello">
    <param name="flashvars" value="name1=hello">
</object>

Again, the swf is embedded fine. I can see the content but the flashvars seems to be null. Here is my flash code that tries to access the flashvars

var flashVars = root.loaderInfo.parameters;
addChild(Util.newLabel("  FlashVars: " + flashVars+" "+root.loaderInfo,0,0,400,100,12,0x0));        


var y=0;
for(var i in flashVars){
    addChild(Util.newLabel("FlashVars:"+i+":"+flashVars[i],0,y+=30,300,100,12,0x0));
}

Util is a custom class I made where newLabel(str:String,x:Number,y:Number,w:Number,h:Number,fontSize:uint, color:uint) is a method that returns a label component with str as its text, x and y as its location, w and h as its dimensions and etc. Im pretty sure that the util stuff works so I would like to know how do I really load flashvars?

I should see a list of variable names when running this code.

PS: The flash code above is coded in the main timeline(im using the flash ide to compile my code), in the first frame, while Util is in a seperate as file. The root object in the first frame in the main timeline is an [object MainTimeline].

Let me know if you need more info. Thanks in advance.

2

There are 2 answers

0
Gokhan Tank On BEST ANSWER

Flash captures the variables either by getting the variables inside the URL or by flashvars and they are key/value pairs. So basically if you had a URL like this:

http://someurl.com/flashmovie.swf?testVariable=101&testVariable2=tank

testVariable is the key and 101 is the value and you could get these key/value pairs as below;

    //creating a textfield for debugging
var _textField:TextField = new TextField();
_textField.autoSize = TextFieldAutoSize.LEFT;
_textField.border = true;
addChild(_textField);

//flashvars code
try {
    var valueString:String;
    var keyString:String;
    var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
    for (keyString in paramObj) {
        valueString = String(paramObj[keyString]);
        _textField.appendText("\t" + keyString + ":\t" + valueString + "\n");
    }
}catch (error:Error) {
    _textField.appendText(error.toString());
}

Hope this helps, Cheers

0
danii On

I just set up an HTML page that embeds a SWF just like yours (in fact, I copied/pasted your code), then in my Flash I put the following (simple) code:

import flash.text.TextField;

var myTextField:TextField = new TextField();    

var flashVars = root.loaderInfo.parameters;

myTextField.text = flashVars["name1"]+"!";

addChild(myTextField);

And when I open the HTML page I get a nice "hello!" on the screen as expected. Obviously I couldn't try your label creator component, but the line would be:

addChild(Util.newLabel(flashVars["name1"]+"!",0,0,400,100,12,0x0) );        

If that doesn't work, I would say you have some sort of mistake inside your Util.newLabel(), my advice would be to try my simple version and see if you can access the flashvars this way (you shouldn't have any trouble) and then check what's going on with the other stuff.

Hope this helps!