|
© Andy Wilson, 1998
Tips and Tricks: Talking Shockwave pt 2
Last month we looked at how to get several Director Shockwave movies talking to one another. This month I want to take the idea a little step further and show how you can get a Flash movie talking to director. A lot of the code is a repetition of last month’s, but with the addition of a few extra lines of code we can begin to achieve our aim of being able to send any message from any Flash or Director movie in your web pages to any other Director movie.
As we did last month, we’ll start by diving right in at the deep end giving you all of the code we are going to use.
HTML / Jscript / VBScript Code
<SCRIPT LANGUAGE=JavaScript>
var InternetExplorer = navigator.appName.indexOf¬
("Microsoft") != -1;
function flashMovie_DoFSCommand(command, args) {
mov = InternetExplorer ? dirMovie : document.dirMovie;
mov.EvalScript(command + ' ' + args);
}
</SCRIPT>
<SCRIPT LANGUAGE=VBScript>
Sub flashMovie_FSCommand(ByVal command, ByVal args)
call flashMovie_DoFSCommand(command, args)
end sub
</SCRIPT>
</HEAD>
<BODY>
<OBJECT
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://active.macromedia.com/flash/¬
cabs/swflash.cab"
ID=flashMovie WIDTH=600 HEIGHT=250>
<PARAM NAME=movie VALUE="button.swf">
<PARAM NAME=quality VALUE=autohigh>
<PARAM NAME=bgcolor VALUE=FFFFFF>
<EMBED SRC="button.swf" NAME=flashMovie
WIDTH=100% HEIGHT=50% QUALITY=autohigh
BGCOLOR=FFFFFF TYPE="application/x-shockwave-flash" ¬
PLUGINSPAGE="http://www.macromedia.com/shockwave/¬
download/index.cgi?¬
P1_Prod_Version=ShockwaveFlash">
</EMBED>
</OBJECT>
<OBJECT NAME="dirMovie"
CLASSID="clsid:166B1BCA-3F9C-11CF-8075-444553540000"
codebase="http://active.macromedia.com/director6/cabs/¬
SW.CAB#version=6,0,0,165"
width="500" height="60" BGCOLOR="OOOOOO">
<param name="SRC" value="movie.dcr">
<EMBED NAME="dirMovie" SRC="send.dcr" WIDTH="240" ¬
HEIGHT="100" ¬
pluginspage="http://www.macromedia.com/shockwave/¬
download/index.cgi?P1_Prod_Version=ShockwaveDirector" ¬
BGCOLOR="#000000">
</OBJECT>
</BODY>
Lingo Code
on calledFunction arg1, arg2
put ("received msg via calledFunction" && arg1 ¬
&& arg2) into field "result"
end
on EvalScript cmdLine
set func = word 1 of cmdLine
delete word 1 of cmdLine
set args = cmdLine
if (args <> EMPTY) then
if (char 1 of args = "(") then delete char 1 of args
if (the last char of args = ")") then
delete the last char of args
end if
set oldDelim = the itemDelimiter
set the itemDelimiter = ","
set argStr = "("
set numArgs = the number of items in args
repeat with x = 1 to numArgs
put QUOTE & item x of args & QUOTE after argStr
if (x <> numARgs) then put "," after argStr
end repeat
put ")" after argStr
set func = func && argStr
set the itemDelimiter = oldDelim
end if
do func
end
Explanation
The purpose of this weeks code is to get a Flash movie talking to its Director partner, so the first thing is to get the two movies embedded in the page. This is done with the two <OBJECT … EMBED> tags. Notice that I’ve used the ID and NAME parameters to call the Flash movie ‘flashMovie’ and the Director movie ‘dirMovie’. Remember that it is important to be careful with naming or calling objects, variables and functions in both JavaScript and VBScript as both languages are case sensitive.
Having embedded the movies, the first thing is to set up the Flash movie itself so that it correctly calls the HTML scripts. To do this, pick the button (or whatever) in your Flash movie that you want to be used to call the scripts: click on the object, select the ‘Action’ option, and then in the panel that opens up, select the ‘Get URL’ option from the pull down menu. Now, enter into the ‘Network URL’ the line ‘FSCommand:’ followed immediately by the name of the handler you want to call in the Director movie. In this case I want to call the handler I have written named ‘calledFunction’, so I entered the line ‘FSCommand:calledFunction’ here. Note that this line doesn’t need any surrounding brackets, double or single, so what you need to enter is actually;
FSCommand:calledFunction
and not;
"FSCommand:calledFunction"
or
‘FSCommand:calledFunction’.
Optionally you can add any parameters you like as a comma separated list in the ‘Target Window’ field. I decided to pass two parameters, ‘param1’ and ‘param2’ by entering the line ‘param1, param2’. These will then be passed as parameters to the lingo ‘calledFunction’ handler in your Director movie. Notice however that no matter what you put in the ‘Target Window’ field the parameters will always arrive at the lingo handler as strings, so make allowances for this when writing your lingo.
The next thing is to write the VBScript handler ‘flashMovie_FSCommand’ and the JavaScript handler ‘flashMovie_DoFSCommand’. These handlers need to be named after the Flash movie that is calling them, along the lines of ‘theNameOfMyFlashMovie_FSCommand’ and ‘theNameOfMyFlashMovie_DoFSCommand’. Interestingly this means that you could write different handlers for any number of Flash movies embedded in your web pages, with each behaving differently. The VBScript handler is there simply to make allowances for Internet Explorer, and needn’t bother us as long as we understand that it’s only purpose is to call the javaScript handler ‘flashMovie_DoFSCommand’.
All that this handler does is to call the handler passed as the ‘command’ argument along with the parameters entered into the ‘Target Window’ field in movie ‘dirMovie’. The first line, which assigns a value to the ‘mov’ variable, is simply to make sure that the code runs in both Netscape and Explorer, and so once again we don’t have to worry about it here. I’ve assumed that you want to call the DirMovie movie, but of course there may be several movies on the web page that you might want to call from your Flash movie. In that case you could use some of the techniques discussed last month to achieve this.
The ‘mov.EvalScript’ line simply calls the EvalScript lingo handler. This ‘EvalScript’ functionality is built into Shockwave as a special gateway for achieving communications between HTML and Shockwave movies and is the correct way to achieve such communications. Notice that you can’t call handler in lingo directly from javaScript, calling perhaps ‘mov.myHandler’ to call the ‘myHandler’ handler in the movie represented by ‘mov’… you have to use ‘EvalScript’ and write an ‘EvalScript’ handler in lingo to handle any messages passed to the movie. The lingo code I’ve used in my own ‘EvalScript’ above is almost identical to the code I used last week, so I shouldn’t need to explain it beyond saying that it’s purpose is merely to unpack the arguments passed to it as strings and turn them into a proper call to the appropriate handler.
Really, that’s about all there is to it. As I said, use the code here in combination with last month’s and you should be able to achieve any combination of message paths you like, passing messages between any combination of Flash and Director movies on the same web page or even - using cgi, the prefs file or cookies - between movies on different pages.
If you have any feedback, ideas for future columns, whatever, send them to me at andyw@dircon.co.uk.
|