FORUMS: list search recent posts

Comunication between 2 SWFs

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
madmac66
Comunication between 2 SWFs
on Feb 9, 2007 at 9:57:01 pm

Wondering if there is a way for 2 SWFs on a HTML page to communicate with each other. Without going to much into it at this time, it would be for sync issues between the 2 movies.

cheers
mm66


Return to posts index

Matt Klundt
Re: Comunication between 2 SWFs
on Feb 9, 2007 at 10:21:30 pm

You can use a Javascript function to do this, however, I'm not sure it would be reliable for syncing the time of two swfs. This code would go in the html page:

<script type="javascript"&rt;
function setVar() {
    document.yourSWF.SetVariable("myValue", 1);
}
</script&rt;


You call the Javascript function in one swf by using the following actionscript:

getURL('javascript:setVar()');


Then on the other swf, you set up a function to watch for a change in the "myValue" variable:

function watchCallback(id, oldval, newval) {
    // Here you place the new value in a text field for display
    _root.readOut.text = newval;
    return newval;
}
_root.watch("myValue", watchCallback);

Matt
--------------------
http://www.mcquillendesign.com
http://www.mattfoxgames.com


Return to posts index

Matt Klundt
Re: Comunication between 2 SWFs
on Feb 9, 2007 at 10:24:15 pm

Oops, typo in last post. Here's the correction:

<script type="javascript">
function setVar() {
document.yourSWF.SetVariable("myValue", 1);
}
</script>

Matt
--------------------
http://www.mcquillendesign.com
http://www.mattfoxgames.com


Return to posts index


madmac66
Re: Comunication between 2 SWFs
on Feb 10, 2007 at 12:07:34 am

Excellent info, even if this does not work out for this particular project it is great to know

thanks a bunch
mm66


Return to posts index

madmac66
Re: Comunication between 2 SWFs
on Feb 10, 2007 at 12:20:20 am

Couple of quick questions...

Would I set the value of myValue by using some kind of argument like this? How else would the value of the variable change for the callback to check?




You call the Javascript function in one swf by using the following actionscript:

getURL('javascript:setVar(25)');



Also, what do the arguments (id, oldVal, newVal) in the callback function do? Where are they passed to/from? newVal obviously goes to the text box. But what is id refering to?

function watchCallback(id, oldval, newval) {
// Here you place the new value in a text field for display
_root.readOut.text = newval;
return newval;
}

thanks for the assistance
mm66


Return to posts index

Matt Klundt
Re: Comunication between 2 SWFs
on Feb 12, 2007 at 4:17:08 pm

Would I set the value of myValue by using some kind of argument like this? Yes.

How else would the value of the variable change for the callback to check? Any way you change the variable would cause the callback to check. I use a variable that has no other use than to wait to be set by Javascript when I use this.

Also, what do the arguments (id, oldVal, newVal) in the callback function do? oldVal can be used to reset the value back to what it used to be, newVal is the new value of the variable.

Where are they passed to/from? They are used within the function, but you don't have to pass anything to it. Setting the watch function takes care of passing the values to it.

what is id refering to? I haven't tried, but I would guess it refers to the variable name. An easy test would be to set its value to the textfield.


On a side note, Sher Ali is right about localConnection being what you are looking for. I've never used it, but it would be worth your time to learn that one instead of my workaround.

Matt
--------------------
http://www.mcquillendesign.com
http://www.mattfoxgames.com


Return to posts index


madmac66
Re: Comunication between 2 SWFs
on Feb 12, 2007 at 4:35:47 pm

excellent, thanks a bunch Matt. will post again when I have something working


Return to posts index

Sher Ali
Re: Comunication between 2 SWFs
on Feb 11, 2007 at 4:05:59 pm

LocalConnection allows multiple swf files to communicate with each other.


Return to posts index

madmac66
Re: Comunication between 2 SWFs
on Feb 12, 2007 at 4:37:12 pm

hmmm, this sounds interesting. I will look into this and see if it is not too complex. Thanks for the tip

mm66


Return to posts index


Sher Ali
Re: Comunication between 2 SWFs
on Feb 12, 2007 at 6:22:20 pm

Ok, here is a short example from the adobe flash help contents.

attach the following block to the sending .swf

var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send("lc_name", "methodToExecute", 5, 7);


and now attach the following code to the receiving .swf file.

this.createTextField("result_txt", 1, 10, 10, 100, 22);
result_txt.border = true;
var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.methodToExecute = function(param1:Number, param2:Number) {
result_txt.text = param1+param2;
};
receiving_lc.connect("lc_name");


two .swf files can communicate if both the files are running on the same client computer, but the two swf files can be running in different applications e.g. one in a browser window and the other one as a standalone .exe file.


Hope it helps you.


--------------------
Sher Ali
http://www.webeyestudio.com


Return to posts index

madmac66
Re: Comunication between 2 SWFs
on Feb 12, 2007 at 9:23:39 pm

great thanks Sher, i'm itching to try this out once I finish designing these things.

I'll keep you posted

thanks again
mm66


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]