HOME       >>       Programming

Pass Variables Between 2 Swf


EinReaper

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?

shadowx

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.


EinReaper

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.


Nabb

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 )

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.

EinReaper

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/



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.