Here's one way.
For examples sake I''ve used a simple movie consisting of only a dynamic textbox with "flashBox" as the variable & a button.
Here's the code used for the button,
button.onPress = function(){
fscommand("getValueOf",["htmlBox","flashBox"]);
}
- button.onPress = function(){
- fscommand("getValueOf",["htmlBox","flashBox"]);
- }
"getValueOf" will be the name of our method so to speak, "htmlBox" will be the ID of the textbox in HTML we want returned & "flashBox" is the variable that will recieve the variable. (note that fscommand allows 1 or 2 arguments, multiple params must be encased in an array)
We select publish settings from the file menu(ctrl+shft+F12), & select "Flash with fscommand" from the "template" dropdown. (make sure you dont publish over an existing page)
This publishes a template we can use to have Flash and Javascript communicate with eachother.
<SCRIPT LANGUAGE=JavaScript>
<!--
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the the FSCommand messages in a Flash movie
function myFSCommand_DoFSCommand(command, args) {
var myFSCommandObj = InternetExplorer ? myFSCommand : document.myFSCommand;
//
//your code here
//
}
// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
document.write('<SCRIPT LANGUAGE=VBScript\> \n');
document.write('on error resume next \n');
document.write('Sub myFSCommand_FSCommand(ByVal command, ByVal args)\n');
document.write(' call myFSCommand_DoFSCommand(command, args)\n');
document.write('end sub\n');
document.write('</SCRIPT\> \n');
}
//-->
</SCRIPT>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ID="myFSCommand" WIDTH="200" HEIGHT="200" ALIGN="">
<PARAM NAME=movie VALUE="myFSCommand.swf">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>
<EMBED src="myFSCommand.swf" quality=high bgcolor=#FFFFFF WIDTH="200" HEIGHT="200" swLiveConnect=true ID="myFSCommand" NAME="myFSCommand" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
</BODY>
</HTML>
- <SCRIPT LANGUAGE=JavaScript>
- <!--
- var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
- // Handle all the the FSCommand messages in a Flash movie
- function myFSCommand_DoFSCommand(command, args) {
- var myFSCommandObj = InternetExplorer ? myFSCommand : document.myFSCommand;
- //
- //your code here
- //
- }
- // Hook for Internet Explorer
- if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
- navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
- document.write('<SCRIPT LANGUAGE=VBScript\> \n');
- document.write('on error resume next \n');
- document.write('Sub myFSCommand_FSCommand(ByVal command, ByVal args)\n');
- document.write(' call myFSCommand_DoFSCommand(command, args)\n');
- document.write('end sub\n');
- document.write('</SCRIPT\> \n');
- }
- //-->
- </SCRIPT>
- <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ID="myFSCommand" WIDTH="200" HEIGHT="200" ALIGN="">
- <PARAM NAME=movie VALUE="myFSCommand.swf">
- <PARAM NAME=quality VALUE=high>
- <PARAM NAME=bgcolor VALUE=#FFFFFF>
- <EMBED src="myFSCommand.swf" quality=high bgcolor=#FFFFFF WIDTH="200" HEIGHT="200" swLiveConnect=true ID="myFSCommand" NAME="myFSCommand" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
- </OBJECT>
- </BODY>
- </HTML>
Now we need to make some changes to this template,
Note the function declaration,
function myFSCommand_DoFSCommand(command, args) {
The part before "_DoFSCommand" MUST match the ID of the movie calling it. If your movies ID is "myMovie" the function would be "myMovie_DoFSCommand".
Now for telling Flash what the value of the box is (you may have noticed there was nothing being assigned in the movie itself ie "var = fscommand")
Inside the function where it says "//your code here" we need theese lines.
if(command = "getValueOf"){
myArgs = args.split(",");
myFSCommandObj.SetVariable(myArgs[1], document.getElementById(myArgs[0]).value);
}
- if(command = "getValueOf"){
- myArgs = args.split(",");
- myFSCommandObj.SetVariable(myArgs[1], document.getElementById(myArgs[0]).value);
- }
The "if('command' == 'getValueOf')" is for separating different function possibilities since we have to use the same _DoFSCommand.. for everything with that movie.
The "myArgs = args.split(',')" is taking our arguments which flash seems to think would be better passed as a string & creating a usable Javascript array out of them.
The template has allready figured out wheather to use "document.movieId" or not with "var myFSCommandObj =..." So we get the value of the requested element & return it with "SetVariable()".
SetVariable takes 2 params, the variable in the movie to set... & the value to pass to it so,
myFSCommandObj.SetVariable(myArgs[1], document.getElementById(myArgs[0]).value);
Will take the first argument passed from flash & find the value of a HTML element with that ID, then it will pass it to the variable named in the second argument.
If you want flash to react to the variable being changed you will need to either set a timeout for flash to wait before trying to use the variable, or setup an object to watch the variable & fire when the variable changes.
Strong with this one, the sudo is.