Solved

AS3: accessing swfObject variable within Flash, help!

Posted on 2009-04-10
24
729 Views
Last Modified: 2013-11-11
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
Comment
Question by:cubical38
  • 12
  • 12
24 Comments
 
LVL 9

Expert Comment

by:asaivan
ID: 24120315
should be root.loaderInfo.parameters.myUrl rather than this
0
 

Author Comment

by:cubical38
ID: 24130191
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
 
LVL 9

Expert Comment

by:asaivan
ID: 24130949
Javascript should be addParam rather then addVariable
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:cubical38
ID: 24131018
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
 
LVL 9

Expert Comment

by:asaivan
ID: 24131107
It looks correct....can I see your HTML and Javascript?
0
 

Author Comment

by:cubical38
ID: 24131132
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
 
LVL 9

Expert Comment

by:asaivan
ID: 24131241
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
 

Author Comment

by:cubical38
ID: 24140467
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
 

Author Comment

by:cubical38
ID: 24140477
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
 
LVL 9

Expert Comment

by:asaivan
ID: 24140521
Again your script needs to be addParam rather than addVariable
0
 

Author Comment

by:cubical38
ID: 24140533
I have tried both with no different result.
0
 
LVL 9

Expert Comment

by:asaivan
ID: 24140553
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
 
LVL 9

Expert Comment

by:asaivan
ID: 24140775
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
 

Author Comment

by:cubical38
ID: 24141522
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
 
LVL 9

Accepted Solution

by:
asaivan earned 500 total points
ID: 24141967
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
 

Author Closing Comment

by:cubical38
ID: 31569063
I just figured it out its so.addVariable instead of addParam or variable...  Geez this is a relief....  Thanks for all the input...  
0
 
LVL 9

Expert Comment

by:asaivan
ID: 24142023
??  How is that different from what you had before?  so.addVariable?
0
 

Author Comment

by:cubical38
ID: 24142050
Im not sure but it is now working...  
0
 
LVL 9

Expert Comment

by:asaivan
ID: 24142070
ok, well, sorry for the wrong info.  Thanks for the points :)
0
 

Author Comment

by:cubical38
ID: 24142509
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
 
LVL 9

Expert Comment

by:asaivan
ID: 24142523
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
 

Author Comment

by:cubical38
ID: 24142613
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
 

Author Comment

by:cubical38
ID: 24142677
NVM, just got it:
myUrl = parent.loaderInfo.parameters.myUrl;
trace(parent.root.loaderInfo.parameters.myUrl);

Thanks Again!
0
 
LVL 9

Expert Comment

by:asaivan
ID: 24142681
well,  I'm not sure if I understand you correctly...why would you need another flashvar?
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question