Bride of Flashenstein
July 5, 2000
|
JavaScript isn't going to go away just yet; and Flash becomes a more
prominent part of the web landscape every day, whether you think of it
as a weed or a flower. Put them together and BAM! - you got your chocolate
in your peanut butter. Flash developers are already doing lots of cool
stuff with Flash/JavaScript, but you have to do a little digging to get
started. Well, here is a good place to break ground...
|
Using Flash with JavaScript (by Example)
This is a quick-and-dirty introduction to Flash with JavaScript. The
reader should already know the basics of both
JavaScript
and Flash. Basically we
are going to walk through a script and Flash movie to get an idea of
how to bridge the two.
First download the
Flashenstein.zip; unzip it and look at
it to get an idea of what we are doing. The concept is
that we are running a "personalized" news site using a cookie
to store the user's preferences. (NOTE: This isn't the most brilliant
scheme for personalized content. This example is devised to get some
ideas going and do something a little more practical with Flash. Don't
spend too much time looking at the Flash file - it is quick and dirty
to say the least).
I usually oppose printing things on paper with religious zeal, but for
a big hunk of sloppy code I like to print it from my favorite HTML
editor
(1st Page 2000), block off the (commented) functions with a
hi-lighter, and write their names in with a ball point pen so I can
quickly follow the structure.
Let's read down the page just like the browser does. First you have
this:
//user configure variables
var retiresAt = new Date("December 31, 2050");
var cookieName="WDVL_FlashCookie";
//init global
var acceptsCookies = false;
Straightforward. retiresAt will become the GMT cookie expiration and
the cookieName will be the name of the cookie, (duh). We initialize a
variable that we will need later.
Everything else is a function until you get to the body tag:
<body onLoad="flashReadCookie();">
<object CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/
shockwave/cabs/flash/swflash.cab#version=4,0,2,0"
ID="flashfile" WIDTH="600"
HEIGHT="450">
<param name="movie" value="cookie_write.swf">
<param name="play" value="false">
<param name="quality" value="high">
<embed NAME="flashfile" SRC="cookie_write.swf"
WIDTH="400" HEIGHT="300" PLAY="false" SWLIVECONNECT="true"
QUALITY="high"
pluginspage="http://www.macromedia.com/shockwave/download/
index.cgi?P1_Prod_Version=ShockwaveFlash">
</embed>
</object>
</body>
The first thing to notice here is that we don't start executing any
script until everything is loaded (onLoad...). The other thing is the
name "flashfile". This is the name of the Flash Player object
we create with the OBJECT and EMBED tags, (document.flashfile in our
example).You must name it using the ID attribute in the OBJECT tag
and the NAME attribute in the EMBED tag. This name can be anything you
want - and needn't correspond to the name of your .swf file.
Contents:
Macabre Machinery
The Bolts
What Do the Tesla Coils Do?
Working example
Bride of Flashenstein
Macabre Machinery
|