SWFObject 2 and flashvars (static embed)
August 3rd, 2009
For quite awhile I’ve been trying to find a way to create “deep links” in Flash files. Here’s the problem:
When building standard HTML pages it’s very easy (obviously) to create links to specific pages so that it’s possible to send someone a link that goes to a particular page in a website. For instance, if I want to send someone to the Home page of the Michelle Wright website I would use this link: http://www.michelle-wright.com. But, if I wanted to send them to her Photo Gallery page I would use this link: http://www.michelle-wright.com/photos.html. I’ve created what we call a “deep link” by adding a slash and the page name to the end of the main URL. Using this syntax it is possible to create a link that goes anywhere in a website, no matter how deep the page is buried and how many folders you have to navigate through. But Flash presents a unique problem. I can build an entire website in Flash, with lots of different pages and sections, but when it comes time to “embed” this Flash file on an HTML page for the web there will only be one HTML page. One page holds the Flash file and though the visitor may navigate throughout the Flash file, they never leave that one HTML page. I’ve never found a way to create an HTML link (like the one above to Michelle Wright’s Photo Gallery) that can cause the Flash file to move from one point to another. Until now.
While investigating into and learning more about SWFObject 2 (a method for embedding Flash .swf files on an HTML page) I discovered “flashvars.” Once I got the concept down it took me a little while to work out all the code necessary (some on the HTML page, some on the Flash file) to create the deep links I was looking for. Here’s how I did it:
1. First the Flash file:
I created a simple Flash file in Flash CS4, using AS3.
a. First I created a “labels” layer and then on the timeline I created three separate frame labels: at frame 10 I entered “ten”; at frame 20 I entered “twenty”; and at frame 30 I entered “thirty”.
b. I then created an “actions” layer and added “stop();” actionScript to each of these frames on that layer.
c. Next I created a “text” layer, adding keyframes in frames 10, 20 and 30. At each one I then entered the words “ten,” “twenty” and “thirty” respectively.
d. Going back to the “actions” layer, in frame 1 I added the following code:
stop();
var theScene:String = root.loaderInfo.parameters["gotoscene"];
if(theScene == “ten”) {
gotoAndStop(”ten”);
} else if(theScene == “twenty”) {
gotoAndStop(”twenty”);
} else if(theScene == “thirty”) {
gotoAndStop(”thirty”);
}
stop();
var theScene:String = root.loaderInfo.parameters["gotoscene"];
if(theScene == “ten”) {
gotoAndStop(”ten”);
} else if(theScene == “twenty”) {
gotoAndStop(”twenty”);
} else if(theScene == “thirty”) {
gotoAndStop(”thirty”);
}
So, what I have done is first stop the movie at this frame. Then the movie checks the root.loaderInfo.parameters for a flashvars variable named “gotoscene.” I use a simple “if/else if” statement to check what was passed into the movie via “gotoscene.” If it is “ten” it moves to frame 10, if it is “twenty” it moves to frame 20 and if it is “thirty” it moves to frame 30. My goal is that by using a query string on the URL I can move the Flash file to the particular frame I want.
2. Now the HTML file:
I created a simple XHTML transitional document and saved it with a .php extension instead of an HTML extension so that I could use PHP within it.
a. Just before the DOCTYPE declaration I inserted this code:
<?php
$gotoscene = NULL;
if (isset($_GET['scene'])) {
$gotoscene = $_GET['scene'];
}
?>
This will check the URL to see if a “scene” variable is being passed to it like this: http://www.digitalvisionmedia.com/examples/scenetest.php?scene=twenty. If the variable and value are being passed in it will store the value in a new variable named “gotoscene.”
b. Now I use SWFObject 2 to embed the Flash file I created in step one via the “static” method.
c. I add the following “param” element to the code:
<param name=”flashvars” value=”gotoscene=<?php echo $gotoscene; ?>” />
So, I’ve just created a flashvars variable with a value that equals the value being passed in by the URL query string. Now this flashvars variable will be passed to the Flash .swf file as it is loaded, be read by the ActionScript in frame one (root.loaderInfo.parameters["gotoscene"];) and move the Flash player to the appropriate frame. Volia!
You can view my finished file here. You’ll note that when the file loads all you see is a blue rectangle. But, if you amend the URL from:
http://www.digitalvisionmedia.com/examples/scenetest.php
to
http://www.digitalvisionmedia.com/examples/scenetest.php?scene=ten
you’ll find that the Flash movie movies to frame 10. We’re still on the same HTML page, but we’ve manipulated the Flash file, via the URL entered, to move to a different point in the movie. If you change the “ten” to either “twenty” or “thirty” you can move to those points as well.
So, what does all this mean to me? Before, if I wanted to create a full artist website in Flash, with things like a Bio page, a Discography page, a Photos page, etc., I would have no way of sending someone directly to one of those pages. I would have had to say, “Go to the Home page and then click on the Bio link.” Now, I can code the Flash and HTML files appropriately and then send someone a link that goes directly to the part of the site I want them to visit. Very cool. Next I’ll look at a way of using the dynamic SWFObject 2 embed method to do this without PHP.

A quick Google search took me to the project’s 

























