Solved

AS3: accessing swfObject variable within Flash, help!

Posted on 2009-04-10
24
721 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now