Jump to content
xisto Community
Sign in to follow this  
EinReaper

Pass Variables Between 2 Swf

Recommended Posts

I have 2 swf files I'll call file1.swf and file2.swf
I use LocalConnection to send a variable between them:

file1.swf

var my_send_lc:LocalConnection = new LocalConnection();	id_sent = "id_value";	my_send_lc.send("lc_name","methodToExecute",id_sent);

file2.swf
var my_receive_lc:LocalConnection = new LocalConnection();my_receive_lc.methodToExecute = function(id_received:Number) {	_root.id_value.text = id_received;};my_receive_lc.connect("lc_name");

This works just fine if both swf files are open in browser tabs (_root.id_value.text gets the value "id_value"), but it doesn't work if I want file1.swf to open file2.swf (_root.id_value.text gets the value "").
I'm using getURL(url, _blank, POST) to open file2.swf.
Does anyone know why this happens or is there another method to send a variable between 2 flash movies where one of them opens the other in a new or in the same window?
Edited by EinReaper (see edit history)

Share this post


Link to post
Share on other sites

Im not a flash expert but can you use something like:flash/flashfile2.swf?variable1=somethingORflash/flashfile2.php?variable1=somethingwhere the SWF is embedded within HTML inside the PHP file?Just a suggestion anyway it seems using the URL would be sensible if flash has support for GET or url variables.

Share this post


Link to post
Share on other sites

I could perhaps use the GET method instead of POST, but then my link would look like this: /localhost/admin/getreferat.html?id_nrows=3&i=4 :) So I'm looking for an alternative. I've found a tutorial that looks promising about a class called SharedObject; apparently this should help save my problem, but I haven't had any luck with it yet.

Share this post


Link to post
Share on other sites

I haven't yet played around with local connections so I can't help you there (without reading off documentation and telling you things that you could easily find out yourself).

Using GET is ridiculous, I did that for a while but since I was using a string of effectively random characters (to represent a level in my game), most symbols had to be escaped and thus the URL was now three times longer :) It's bad to put so much in GET - at one point I had too much data in there that the URL length was exceeded, and it was at that point I switched to using POST.

Shared objects are fairly simple to use and should be able to solve your problems. The best tutorial is here (in my opinion) but I'll briefly describe it here:

ShObj = SharedObject.getLocal("filename");ShObj.data.name = "Nab";ShObj.flush()
(flush() saves the data to the file, if you don't call it then the data is only saved when you close the first swf!)
Then in the other swf you can do something like
ShObj = SharedObject.getLocal("filename");if(ShObj.data.name!=null){trace(ShObj.data.name);}else{trace("No data :(")}
This isn't that good if both windows are going to be open unless you're going to either set an interval which continuously checks for changes, or force the user to click a button when he has updated in the other swf.

I haven't come across using getURL() with POST data directly, and via documentation here, it seems that all variables are sent and that's pretty stupid.
Another problem with this is each time you call getURL(), a new window will open. This may not seem like a problem, but if the system is for something like a level editor and corresponding testing area, and each time you hit 'test level' in the editor a new window came up for testing, it'd get pretty annoying. I managed to come up with something last time with javascript to get around this.

Here's my method: (arg - when I made this last time I used double-quotes everywhere and thus had to escape all of them :P)

url = "<html><head><title>Redirecting...</title></head><body>Redirecting...<form name='aForm' method='POST' action='http://your.website.com/folder/file.php'><input type='hidden' name='stuff' value='" + escape (stuff) + "'></form><script language='JavaScript'>document.aForm.submit();</script></body></html>";	getURL ("java script:win = window.open('','Redirecting..',toolbar=0,status=0,scrollbar=0,resizable=0,width=550,height=550);win.document.open();win.document.write(unescape('" + escape (escape (url)) + "'));win.document.close()");

Using this, the same window will be used each time you press some button that opens the other one.

And in file.php, you'd need something like: (I hear this is a bad way to do this - i.e. embed swf's)

<?php$stuff = $_POST['stuff'];$heredoc  = <<<LOLSTUFF<html><head><title>This is a title!</title></head><body bgcolor="#ffffff"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="550" id="file2" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="file2.swf" /><param name=FlashVars value="stuff=LOLSTUFF;$heredoc .=  $stuff;$heredoc .=  '><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="file2.swf" FlashVars="stuff=';$heredoc .=  $stuff;$heredoc .=  ' quality="high" bgcolor="#ffffff" width="550" height="550" name="file2" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://http://www.adobe.com/special/errorpages/404.html; /></object>';echo $heredoc;?>

Another way would be to directly echo all that stuff with document.write when you call getURL(), but that's really really ugly.

Also, I probably escaped something too much, I tend to escape things a lot...

edit) note: using FlashVars instead of using file2.swf?stuff=stuff saves bandwidth as far as I know - you'll need to load file2.swf again if you have ?stuff=morestuff a second time.
Edited by Nabb (see edit history)

Share this post


Link to post
Share on other sites

I've been researching about these two classes. It seems that LocalConnection works only when both swf files are open at the same time and this makes it useless to me.
The SharedObject helped me a lot with this and it's very easy to use. I had to use the optional source/destination parameter because, for some reason, it didn't work otherwise. I mean the first swf (the one that was creating the SharedObject) could read the data, but the second swf could not.

Thanks for the turorial. I've also found a good tutorial on the SharedObject class if anyone else is interested: http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.