Flash Var

UNCOMMENTED

var buttonText:TextField = new TextField();
buttonText.text = “OPEN PDF”;
buttonText.autoSize = TextFieldAutoSize.CENTER;
buttonText.selectable = false;
buttonText.border = true;
addChild(buttonText);

var myPathLink:String = stage.loaderInfo.parameters.myPathLink;

var myfile:String = “myFlashVarDocument.pdf”;

buttonText.addEventListener(MouseEvent.MOUSE_DOWN, printPDF);
function printPDF(Event:MouseEvent):void
{
navigateToURL(new URLRequest(myPathLink + myfile));
}
stop();

// see below for code you’ll need in your html document

COMMENTED

/* A FlashVar allows you to pass variables between your
swf and an html page.

NOTE: clean script for setting up a FlashVar was impossible
for Coding Cat to find on the world wide web, and books on
the subject are long on description and short on practical instruction.
After paid consultations with two different programmers,
neither of which gave fully adequate solutions, Coding Cat
wired together parts of code from each and is able to offer
the working script here.
This may be the only copy-and-paste reference you’ll find
for creating a FlashVar.

This script sets up a variable in Flash that reads a name-value pair
from your html page. That value is the path to a pdf file.
This allows you to change the file-path in the html file.
The pdf file itself is specified by a variable inside your swf file.
*/

// IN FLASH:
// sets up a button for opening a pdf file, and adds to stage

var buttonText:TextField = new TextField();
buttonText.text = “OPEN PDF”;
buttonText.autoSize = TextFieldAutoSize.CENTER;
buttonText.selectable = false;
buttonText.border = true;
addChild(buttonText);

/* instantiates the variable “myPathLink” which will link to a variable by
the same name in your html document, allowing you to easily change the path,
in your html page, to access a file that your swf is looking for.
HERE’S THE MAGIC:  */
var myPathLink:String = stage.loaderInfo.parameters.myPathLink;

// this creates a string variable for a pdf file name (to use in URLRequest below):

var myfile:String = “myFlashVarDocument.pdf”;

/* adds listener to button to retrieve the pdf file.
uses the file path you’ll place in your html document using the myPathLink variable
which is linked to both your html file and your swf file */

buttonText.addEventListener(MouseEvent.MOUSE_DOWN, printPDF);
function printPDF(Event:MouseEvent):void
{
/* this URLRequest will pull the value of the myPathLink variable from your html page.
the value of the pdf file to retrieve is pulled from the myfile variable above */

navigateToURL(new URLRequest(myPathLink + myfile));
}
stop();

/* WHAT TO ADD TO YOUR HTML FILE:

1. Publish your Flash file (generates an html file).

2. Open the html file in your html editor and look for the four commands below
(probably near the bottom of your html page).


line 291:   ‘src’, ‘myFlashVarExample’,

line 306:   ‘movie’, ‘myFlashVarExample’,
line 314:   param name=”movie” value=”myFlashVarExample.swf”
line 314:   embed src=”myFlashVarExample.swf”

(the line numbers above were in Coding Cat’s html editor
- yours may be different)
(“myFlashVarExample”  is the name of Coding Cat’s swf file
- yours may be different)

HERE’S THE REST OF THE MAGIC:
Add this code to each of the four html commands starting to the left of the close-quote:
?myPathLink=http://justthecodeplease.com/
Here, the “myPathLink” variable is assigned the “justthecodeplease.com/” path
which is where the “myFlashVarDocument.pdf” file is located.

Now, each of these html commands should appear exactly like this:
‘src’, ‘myFlashVarExample?myPathLink=http://justthecodeplease.com/’,

‘movie’, ‘myFlashVarExample?myPathLink=http://justthecodeplease.com/’,

param name=”movie” value=”myFlashVarExample.swf?myPathLink=http://justthecodeplease.com/”

embed src=”myFlashVarExample.swf?myPathLink=http://justthecodeplease.com/”

The variable “myPathLink” can be changed to point to your own website and subfolder.

Make sure that your flash variable “myfile” is assigned the same name as the file you place on your website.

NOTE: The forward slash after justthecodeplease.com is critical for separating the path name from the file name.

If you get a 1093 error in Flash, retype the four quotation marks in the uncommented script
*/

URL Link

UNCOMMENTED

stage.addEventListener(MouseEvent.CLICK, gotoURL);
function gotoURL(evt:MouseEvent):void
{
var JTCP:URLRequest = new URLRequest(http://www.justthecodeplease.com);
navigateToURL(JTCP, “_blank”);
}

COMMENTED

// establishes a link to a specific URL

/* adds event listener to stage so when the stage
is clicked a new webpage is launched */
stage.addEventListener(MouseEvent.CLICK, gotoURL);
function gotoURL(evt:MouseEvent):void
{
// instantiates a url request object and assigns the url address
var JTCP:URLRequest = new URLRequest(http://www.justthecodeplease.com);
navigateToURL(JTCP, “_blank”);
// “_blank” launches a new browser window
}