Link to home
Start Free TrialLog in
Avatar of cubical38
cubical38

asked on

AS3: accessing swfObject variable within Flash, help!

Okay, I have converted but I am a little stuck on the as3 syntax to get everything to communicate. Could you gander at my code and give some pointers?
And btw thanks for all the input thus far...

HTML:
 
<script type="text/javascript" src="swfobject.js"></script>
<div id="flashcontent"> This text is replaced by the Flash movie. </div>
<script type="text/javascript">
       var so = new SWFObject("Hilti360.swf", "Hilti360", "540", "360", "8", "#ffffff");
       so.addVariable("myUrl", "index.html");
       so.write("flashcontent");
</script>
</body>
 
 
AS3:
 
    var xmlFile:String;
    xmlFile = this.loaderInfo.parameters.myUrl;
    var myUrl:String;
    //trace(currenturl);
    //var xmlFile:String;
    if (myUrl == "index-FR.html") {
        xmlFile = "content/FR.xml";
        trace("FR");
    } else if (myUrl == "index.html") {
        xmlFile = "content/EN.xml";
        trace("EN");
    } else {
        xmlFile = "content/PT.xml";
        trace("PT");
    }
    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
 
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
 
    xmlLoader.load(new URLRequest(xmlFile));
 
    function LoadXML(e:Event):void {
        trace("LoadXML");
        xmlData = new XML(e.target.data);
        ParseContent(xmlData);
    }

Open in new window

Avatar of asaivan
asaivan
Flag of United States of America image

should be root.loaderInfo.parameters.myUrl rather than this
Avatar of cubical38
cubical38

ASKER

Okay, with this code listed below the trace for my variables is being returned as null.  There is something I am not doing, either in the html or the AS3,  that is causing them to not get passed into Flash and therefore are null...  Do you see something that I am doing here that would cause this?  I havent changed anything from the html above...

Thanks Again!
        var xmlFile:String;
	xmlFile = root.loaderInfo.parameters.myUrl
	trace(myUrl);
	trace(xmlFile);
 
 
	var myUrl:String;
	//trace(currenturl);
	//var xmlFile:String;
	if (myUrl == "index-FR.html") {
		xmlFile = "content/FR.xml";
		trace("FR");
	} else if (myUrl == "index.html") {
		xmlFile = "content/EN.xml";
		trace("EN");
	} else {
		xmlFile = "content/PT.xml";
		trace("PT");
	}
	var xmlLoader:URLLoader = new URLLoader();
	var xmlData:XML = new XML();
 
	xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
 
	xmlLoader.load(new URLRequest(xmlFile));
 
	function LoadXML(e:Event):void {
		trace("LoadXML");
		xmlData = new XML(e.target.data);
		ParseContent(xmlData);
	}

Open in new window

Javascript should be addParam rather then addVariable
trace(myUrl);
trace(xmlFile);

still return: null...  My variables are not being read in.
       
Something is not correct here:
        var xmlFile:String;
        xmlFile = root.loaderInfo.parameters.myUrl
        trace(myUrl);
        trace(xmlFile);

trace(root.loaderInfo.parameters.myUrl);

returns: undefined

But I cant seem to place correct syntax in...


It looks correct....can I see your HTML and Javascript?
Yea here it is.  This is the first time I have used swfobject for embedding flash...

Thanks!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Hili 360</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<body>
<script type="text/javascript" src="swfobject.js"></script>
<div id="flashcontent"> This text is replaced by the Flash movie. </div>
<script type="text/javascript">
   	var so = new SWFObject("Hilti360.swf", "Hilti360", "540", "360", "8", "#ffffff");
   	so.addParam("myUrl", "index.html");
   	so.write("flashcontent");
</script>
</body>
</html>

Open in new window

I notice for instance that you're targeting flash player 8 in your embed code.  But to use AS3 you have to be targeting at least FP9.  I don't know if that's got anything to do with it, but you should change it.  
Here is the latest I have, but I am still having the issue of only the default case appearing.  If I remove the default then I receive the error:
TypeError: Error #2007: Parameter url must be non-null.

This would tell me that either the variable or the url is not being read in.  Please review my code as I am not sure what to do next...

the HTML is index-PT.html and is saved on the same level as the flash piece...  When viewed this should show content/PT.xml, yet it reveals the default content/EN.xml instead.

Please, can you see anything that doesn't match up?

Thanks!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Hili 360</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<body>
<script type="text/javascript" src="swfobject.js"></script>
<div id="flashcontent"> This text is replaced by the Flash movie. </div>
<script type="text/javascript">
   	var so = new SWFObject("Hilti360.swf", "Hilti360", "540", "360", "9", "#ffffff");
   	so.addVariable("myUrl", "index-PT.html");
   	so.write("flashcontent");
</script>
</body>
</html>
 
//--------AS3-----------
 
        var xmlFile:String;
	trace(xmlFile);
 
	var myUrl:String;
	trace(myUrl);
 
	//myUrl = root.loaderInfo.parameters.myUrl;
	//trace(root.loaderInfo.parameters.myUrl);
	root.loaderInfo.addEventListener(Event.COMPLETE, onLoaderInfoComplete);
 
	function onLoaderInfoComplete(e:Event):void {
		myUrl = root.loaderInfo.parameters.myUrl;
		trace(root.loaderInfo.parameters.myUrl);
		switch (myUrl) {
			case "index-FR.html" :
				xmlFile = "content/FR.xml";
				trace("FR");
				break;
			case "index-PT.html" :
				xmlFile = "content/PT.xml";
				trace("PT");
				break;
			/*default :
				xmlFile = "content/EN.xml";
				trace("EN");
				break;*/
		}
 
		var xmlLoader:URLLoader = new URLLoader();
		var xmlData:XML = new XML();
 
		xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
 
		xmlLoader.load(new URLRequest(xmlFile));
		function LoadXML(e:Event):void {
			trace("LoadXML");
			xmlData = new XML(e.target.data);
			ParseContent(xmlData);
		}
	}

Open in new window

AS3 without commented default:
        var xmlFile:String;
	trace(xmlFile);
 
	var myUrl:String;
	trace(myUrl);
 
	//myUrl = root.loaderInfo.parameters.myUrl;
	//trace(root.loaderInfo.parameters.myUrl);
	root.loaderInfo.addEventListener(Event.COMPLETE, onLoaderInfoComplete);
 
	function onLoaderInfoComplete(e:Event):void {
		myUrl = root.loaderInfo.parameters.myUrl;
		trace(root.loaderInfo.parameters.myUrl);
		switch (myUrl) {
			case "index-FR.html" :
				xmlFile = "content/FR.xml";
				trace("FR");
				break;
			case "index-PT.html" :
				xmlFile = "content/PT.xml";
				trace("PT");
				break;
			default :
				xmlFile = "content/EN.xml";
				trace("EN");
				break;
		}
 
		var xmlLoader:URLLoader = new URLLoader();
		var xmlData:XML = new XML();
 
		xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
 
		xmlLoader.load(new URLRequest(xmlFile));
		function LoadXML(e:Event):void {
			trace("LoadXML");
			xmlData = new XML(e.target.data);
			ParseContent(xmlData);
		}
	}

Open in new window

Again your script needs to be addParam rather than addVariable
I have tried both with no different result.
Can you send me your FLA or AS code and HTML file?  Basically, let me have a look at your code and project as a whole.  You can email me at support@globalchangemultimedia.net
Hi Josh,

I'm getting a damaged archive message when I try to unzip.  Do I understand correctly that you've got a main SWF which loads another SWF into it?  Is the secondary SWF the one in which you want your variables loaded?  If so that would be the problem.  You have to have the main SWF get the loaded variables from the HTML page.
Okay lets try this I will send you the AS3 and my html files.  There are a lot of classes that make this piece work so it will get bulky if I send them all.  I have the main swf within the html files as HiltiCallouts.swf which is the swf or fla that has this code on it ( I have bypassed the Hilti360swf until all is working properly as the most of the xml is on this swf anyways).  These files are all on the same level.  The xml files are all located in a folder on the same level named content.  When I comment out the default case then the following error occurs:
TypeError: Error #2007: Parameter url must be non-null.
...which would lead me to believe that none of the variables are being read.  I have also sent you all of the html files, only differing in the index... variable...

Let me know if anything else would be needed...  BTW thanks alot!


//------------AS3--------------
 
 
	var xmlFile:String;
	trace(xmlFile);
 
	var myUrl:String;
	trace(myUrl);
 
	//myUrl = root.loaderInfo.parameters.myUrl;
	//trace(root.loaderInfo.parameters.myUrl);
	root.loaderInfo.addEventListener(Event.COMPLETE, onLoaderInfoComplete);
 
	function onLoaderInfoComplete(e:Event):void {
		myUrl = root.loaderInfo.parameters.myUrl;
		trace(root.loaderInfo.parameters.myUrl);
		switch (myUrl) {
			case "index-FR.html" :
				xmlFile = "content/FR.xml";
				trace("FR");
				break;
			case "index-PT.html" :
				xmlFile = "content/PT.xml";
				trace("PT");
				break;
			default :
				xmlFile = "content/EN.xml";
				trace("EN");
				break;
		}
 
		var xmlLoader:URLLoader = new URLLoader();
		var xmlData:XML = new XML();
 
		xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
 
		xmlLoader.load(new URLRequest(xmlFile));
		function LoadXML(e:Event):void {
			trace("LoadXML");
			xmlData = new XML(e.target.data);
			ParseContent(xmlData);
		}
	}

Open in new window

index-FR.html.txt
index-PT.html.txt
index.html.txt
ASKER CERTIFIED SOLUTION
Avatar of asaivan
asaivan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just figured it out its so.addVariable instead of addParam or variable...  Geez this is a relief....  Thanks for all the input...  
??  How is that different from what you had before?  so.addVariable?
Im not sure but it is now working...  
ok, well, sorry for the wrong info.  Thanks for the points :)
I may need to add this as another question but now that I have the callouts.swf working how can I get this to work within the main container hilti360.swf?
ok, nah, we can do it here.

Just store that info in a variable.  Like you have myUrl = root.loaderInfo.... etc.

You can access that variable from the container SWF by calling parent.myUrl from the loaded SWF.  
Okay so:
myUrl = root.loaderInfo.parameters.myUrl;
trace(root.loaderInfo.parameters.myUrl);

...sits on the Hilti360.swf (the main swf), and:

??
myUrl = root.loaderInfo.parameters.myUrl;
trace(root.loaderInfo.parameters.myUrl);

...sits in the loaded (HiltiCallouts.swf)

...and there needs to be another:
 so.addVariable("myUrl", "index-FR.html");
?
NVM, just got it:
myUrl = parent.loaderInfo.parameters.myUrl;
trace(parent.root.loaderInfo.parameters.myUrl);

Thanks Again!
well,  I'm not sure if I understand you correctly...why would you need another flashvar?