Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 748
  • Last Modified:

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

0
cubical38
Asked:
cubical38
  • 12
  • 12
1 Solution
 
asaivanCommented:
should be root.loaderInfo.parameters.myUrl rather than this
0
 
cubical38Author Commented:
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

0
 
asaivanCommented:
Javascript should be addParam rather then addVariable
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cubical38Author Commented:
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...


0
 
asaivanCommented:
It looks correct....can I see your HTML and Javascript?
0
 
cubical38Author Commented:
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

0
 
asaivanCommented:
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.  
0
 
cubical38Author Commented:
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

0
 
cubical38Author Commented:
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

0
 
asaivanCommented:
Again your script needs to be addParam rather than addVariable
0
 
cubical38Author Commented:
I have tried both with no different result.
0
 
asaivanCommented:
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
0
 
asaivanCommented:
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.
0
 
cubical38Author Commented:
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
0
 
asaivanCommented:
OK, I checked out this HTML.  It's working fine.  By the way, I was wrong about addParam, it IS addVariable.  Sorry, it's been awhile since I got in to the nitty gritty of SwfObject.
 
I really don't understand WHY this is not working.  I've looked at the code, and it's correct.  The main problem is that you're not getting that variable in to the SWF, which you already know.

What I would suggest now is to go back and try rebuilding this a step at a time.  Start with a scrap FLA or AS file, and do the root.loaderInfo.parameters.myUrl thing, and see what you get.  Then from there, try to identify what's gone wrong.
0
 
cubical38Author Commented:
I just figured it out its so.addVariable instead of addParam or variable...  Geez this is a relief....  Thanks for all the input...  
0
 
asaivanCommented:
??  How is that different from what you had before?  so.addVariable?
0
 
cubical38Author Commented:
Im not sure but it is now working...  
0
 
asaivanCommented:
ok, well, sorry for the wrong info.  Thanks for the points :)
0
 
cubical38Author Commented:
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?
0
 
asaivanCommented:
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.  
0
 
cubical38Author Commented:
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");
?
0
 
cubical38Author Commented:
NVM, just got it:
myUrl = parent.loaderInfo.parameters.myUrl;
trace(parent.root.loaderInfo.parameters.myUrl);

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now