Load External SWF File

UNCOMMENTED

var image1Loader:Loader = new Loader();
var image01Url:URLRequest = new URLRequest(‘image01.swf’);
image1Loader.load(image01Url);
addChild(image1Loader);

COMMENTED

// instantiates Loader
var
image1Loader:Loader = new Loader();
// assigns swf file to url request
var
image01Url:URLRequest = new URLRequest(‘image01.swf’);
// loads the swf file
image1Loader.load(image01Url);
//  displays the swf file on the stage
addChild(image1Loader);

// NOTE: For this script, you will need for the swf file to be in the same
folder as this FLA file. Make sure your swf file name is in the URLRequest line above.

Context Menu

UNCOMMENTED
var shape1:Shape = new Shape();
shape1.graphics.beginFill(0xB4A4F4);
shape1.graphics.drawRect(0,0,40,40);
shape1.graphics.endFill();
var mc1:MovieClip = new MovieClip();
mc1.addChild(shape1);
mc1.x = 40; mc1.y = 40;
addChild(mc1);

var shape2:Shape = new Shape();
shape2.graphics.beginFill(0xBADFB9);
shape2.graphics.drawRect(0,0,40,40);
shape2.graphics.endFill();
var mc2:MovieClip = new MovieClip();
mc2.addChild(shape2);
mc2.x = mc1.x; mc2.y = mc1.y * 3;
addChild(mc2);

var stringForLink:String;
var dateNow:Date = new Date();
var preString:String = “Context Menu for: “;
var postString:String = “.”;

var myContextMenu = new ContextMenu();
myContextMenu.hideBuiltInItems();

var copyrightNotice:ContextMenuItem = new ContextMenuItem(“© “ + dateNow.fullYear + ” justthecodeplease.com”);
var catNotice:ContextMenuItem = new ContextMenuItem(“Developed by Coding Cat”, true, false, true);
var closeMenu:ContextMenuItem = new ContextMenuItem(“Close This Menu”, false, true, true);
var mySiteLink:ContextMenuItem = new ContextMenuItem(stringForLink);

copyrightNotice.separatorBefore = true;
copyrightNotice.enabled = false;

stringForLink = preString + ” Square One” + postString;
myContextMenu = new ContextMenu();
myContextMenu.hideBuiltInItems();
mySiteLink = new ContextMenuItem(stringForLink);
myContextMenu.customItems.push(mySiteLink, closeMenu, catNotice, copyrightNotice);
mySiteLink.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function() { navigateToURL(new URLRequest(http://www.justthecodeplease.com), “blank”);});
mc1.contextMenu = myContextMenu;

stringForLink = preString + ” Square Two” + postString;
myContextMenu = new ContextMenu();
myContextMenu.hideBuiltInItems();
mySiteLink = new ContextMenuItem(stringForLink);
myContextMenu.customItems.push(mySiteLink, closeMenu, catNotice, copyrightNotice);
mySiteLink.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function() { navigateToURL(new URLRequest(http://www.justthecodeplease.com), “blank”);});
mc2.contextMenu = myContextMenu;

stop();


COMMENTED

/* A Context Menu is what you see when you right-click
on a Flash presentation. This menu can be used to provide
information to the user, and as links to other URLs or files.

This script adds context menus to two objects on the stage.
*/

// dynamically adds two rectangles to stage
var shape1:Shape = new Shape();
shape1.graphics.beginFill(0xB4A4F4);
shape1.graphics.drawRect(0,0,40,40);
shape1.graphics.endFill();
var mc1:MovieClip = new MovieClip();
mc1.addChild(shape1);
mc1.x = 40; mc1.y = 40;
addChild(mc1);

var shape2:Shape = new Shape();
shape2.graphics.beginFill(0xBADFB9);
shape2.graphics.drawRect(0,0,40,40);
shape2.graphics.endFill();
var mc2:MovieClip = new MovieClip();
mc2.addChild(shape2);
mc2.x = mc1.x; mc2.y = mc1.y * 3;
addChild(mc2);

/* instantiates strings to be used in context menu.
 stringForLink is a string of other concatenated strings
 and will be used as a URL link under mySiteLink below */
var stringForLink:String;
// dateNow gets the current date
var dateNow:Date = new Date();
// a string to be used in the stringForLink string
var preString:String = “Context Menu for: “;
// another string to be used in the stringForLink string
var postString:String = “.”;

// instantiates your context menu, which will replace the default context menu
var myContextMenu = new ContextMenu();
// makes sure default menu items are hidden (optional)
myContextMenu.hideBuiltInItems();

/* instantiates, but does not display, the menu items
(does not establish the order in which the menu items appear) */
var copyrightNotice:ContextMenuItem = new ContextMenuItem(“© “ + dateNow.fullYear + ” justthecodeplease.com”);
/* four format options in parenthesis (your string, insert separator (ruled line) above Boolean,
 enable/disable clickable Boolean, visible/invisible Boolean */
var catNotice:ContextMenuItem = new ContextMenuItem(“Developed by Coding Cat”, true, false, true);
var closeMenu:ContextMenuItem = new ContextMenuItem(“Close This Menu”, false, true, true);
// no format options added to next item (defaults to no-separator, clickable and visible)
var mySiteLink:ContextMenuItem = new ContextMenuItem(stringForLink);

/* another method for adding a separator (ruled line) above a menu item.
 one default separator will appear and separate your last item from
 the default items below it, which can’t be suppressed */
copyrightNotice.separatorBefore = true;
// another method for disabling menu items so they’re not clickable
copyrightNotice.enabled = false;

/* populates the stringForLink string variable with 3 concatenated strings,
  one which is ” Square One” to identify the object it’s activated with */
stringForLink = preString + ” Square One” + postString;
// necessary to add a new context menu with it’s own features (strings, formatting, links)
myContextMenu = new ContextMenu();
myContextMenu.hideBuiltInItems();
mySiteLink = new ContextMenuItem(stringForLink);
// this adds your strings to the context menu in the order shown in the parenthesis
myContextMenu.customItems.push(mySiteLink, closeMenu, catNotice, copyrightNotice);
// makes the mySiteLink string link to an URL (or to a file)
mySiteLink.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function() { navigateToURL(new URLRequest(http://www.justthecodeplease.com), “blank”);});
/* ties this context menu to the mc1 object.
 for context menu to appear from anywhere the stage is clicked, use: this.contextMenu = myContextMenu; */
mc1.contextMenu = myContextMenu;

// another context menu for Square Two:
stringForLink = preString + ” Square Two” + postString;
myContextMenu = new ContextMenu();
myContextMenu.hideBuiltInItems();
mySiteLink = new ContextMenuItem(stringForLink);
myContextMenu.customItems.push(mySiteLink, closeMenu, catNotice, copyrightNotice);
mySiteLink.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function() { navigateToURL(new URLRequest(http://www.justthecodeplease.com), “blank”);});
mc2.contextMenu = myContextMenu;

stop();

// Note: if you get a 1093: Syntax error, please retype all quotation marks in your editor

Dynamic String / Object Array

UNCOMMENTED

var stringVariable:String;
var mcArray:Array = new Array();
var nameArray:Array = ['first', 'second', 'third', 'forth', 'fifth'];
var n:int =0;
while (n < nameArray.length)
{
var mysquare:Shape = new Shape();
mysquare.graphics.beginFill(0x00ff00);
mysquare.graphics.drawRect(0, 0, 20, 20);
mysquare.graphics.endFill();
mcArray[n] = new MovieClip();
mcArray[n].addChild(mysquare);

mcArray[n].name = nameArray[n] + (new String(stringVariable = ‘Movie’))
trace(mcArray[n].name + ‘ is an ‘ + mcArray[n]);
addChild(mcArray[n]);
mcArray[n].y = (mcArray[n].height * 2) * n;
n++;
}

if(n == nameArray.length){
if(mcArray[3].name == ‘forthMovie’){
mcArray[3].alpha = .2;
}
}

COMMENTED

/* this script shows one way to associate a series of string values
with a series of movieclip objects  */

// instantiates a string variable with a null value
var stringVariable:String;
// instantiates a new array to hold movieclip objects
var mcArray:Array = new Array();
/* instantiates a new array and populates it with string values
to be associated with movieclip names  */
var nameArray:Array = ['first', 'second', 'third', 'forth', 'fifth'];
/* while-loop for dynamically creating string variables and dynamically creating movieclips in the same quantity as the elements in the array: nameArray */
var n:int =0;
while (n < nameArray.length)
{
 // instantiates one new green square with each cycle through the while-loop
var mysquare:Shape = new Shape();
mysquare.graphics.beginFill(0x00ff00);
mysquare.graphics.drawRect(0, 0, 20, 20);
mysquare.graphics.endFill();

 // instantiates a new MovieClip object with each cycle through the while-loop
mcArray[n] = new MovieClip();
 // adds each new square shape to each new MovieClip object
mcArray[n].addChild(mysquare);

 /* TO USE A SYMBOL FROM YOUR OBJECT LIBRARY, INSTEAD OF THE “mysquare” SHAPE ABOVE:
 1. right click your symbol name in the library list and make sure “Export for ActionScript”
 is selected, and type into the Class input box “yourSymbol”
 2. comment out the four lines above containing references to “mysquare”
 3. replace them with the one line below to place your symbol into a container object: 
 var yourContainer:yourSymbol = new yourSymbol();

 4. change the line:
 mcArray[n].addChild(mysquare);
 to:
 mcArray[n].addChild(yourContainer); 
 */

// concatenates a nameArray string element to the string variable ‘Movie’
mcArray[n].name = nameArray[n] + (new String(stringVariable = ‘Movie’))
 // outputs the concatenated strings and the object type of each mcArray element
trace(mcArray[n].name + ‘ is an ‘ + mcArray[n]);
 // adds each new MovieClip to the stage
addChild(mcArray[n]);
 // spaces MovieClips vertically by a distance equal to their height
mcArray[n].y = (mcArray[n].height * 2) * n;
 // adds 1 to previous value of n
n++;
}

/* EXAMPLE OF A STRING VARIABLE USED AS A REFERENCE TO A MOVIECLIP ELEMENT FROM THE ARRAY
if n equals the length of the nameArray, and then if an array string equals ‘forthMovie’,
then give the 4th MovieClip element an alpha value of .2 */
if(n == nameArray.length){
if(mcArray[3].name == ‘forthMovie’){
mcArray[3].alpha = .2;
}
}
/* note that the movieclip in the example above must be called by its
array reference: “mcArray[3]” in order to change it’s alpha value,
and that “mcArray[3].name” is only a virtual name constructed using
an associative array.

DISPLAYS IN OUTPUT WINDOW:
firstMovie is an [object MovieClip]
secondMovie is an [object MovieClip]
thirdMovie is an [object MovieClip]
forthMovie is an [object MovieClip]
fifthMovie is an [object MovieClip]

Note: if you get a 1093: Syntax error, please retype all quotation marks in your editor
*/

Dynamic String Variables

UNCOMMENTED

var dynamicArray:Array = new Array();
var stringVariable:String;
var i:int = 0;
while(i < 5)
{
dynamicArray.push(new String(stringVariable = ‘myVariable’) + i);
trace(dynamicArray[i]);
i++;
}

trace
();
var dynamicArray2:Array = new Array();
var nameArray:Array = ['first', 'second', 'third', 'fourth', 'fifth'];
var n:int =0;
while (n < 5)
{
dynamicArray2.push(nameArray[n] + (new String(stringVariable = ‘Variable’)));
trace(dynamicArray2[n]);
n++;
}

COMMENTED

//
two examples of generating dynamic string variables

var dynamicArray:Array = new Array();
// instantiates a string variable with no (null) value
var
stringVariable:String;
var i:int = 0;
// while-loop used to generate 5 dynamic string variables
while
(i < 5)
{
// adds the integer value of i to the dynamically generated string values
dynamicArray.push(new String(stringVariable = ‘myVariable’) + i);
trace(dynamicArray[i]);
i++;
}

trace();
var dynamicArray2:Array = new Array();
// instantiates & populates an array with elements to be used in dynamic string variables
var nameArray:Array = ['first', 'second', 'third', 'fourth', 'fifth'];
var n:int =0;
while (n < 5)
{
// adds the string variable “Variable” to the array elements from the nameArray above
dynamicArray2.push(nameArray[n] + (new String(stringVariable = ‘Variable’)));
trace(dynamicArray2[n]);
n++;
}

/* OUTPUT DISPLAYED:
myVariable0
myVariable1
myVariable2
myVariable3
myVariable4

firstVariable
secondVariable
thirdVariable
fourthVariable
fifthVariable
*/

Scale Object

UNCOMMENTED

var greenShape:Shape = new Shape();
greenShape.graphics.beginFill(0x00FF00);
greenShape.graphics.drawRoundRect(30, 30, 100, 100, 20);
greenShape.graphics.endFill();
var greenSprite:Sprite = new Sprite();
greenSprite.addChild(greenShape);
addChild(greenSprite);

greenSprite.addEventListener(MouseEvent.CLICK, scaleIt)
function scaleIt(evt:MouseEvent):void
{
greenSprite.scaleX = greenSprite.scaleY = .5;
}

COMMENTED

// draws a square and scales it 50%

// draws square and adds to stage
var greenShape:Shape = new Shape();
greenShape.graphics.beginFill(0x00FF00);
greenShape.graphics.drawRoundRect(30, 30, 100, 100, 20);
greenShape.graphics.endFill();
var greenSprite:Sprite = new Sprite();
greenSprite.addChild(greenShape);
addChild(greenSprite);

// adds event listener to square to rescale square
greenSprite.addEventListener(MouseEvent.CLICK, scaleIt)
function scaleIt(evt:MouseEvent):void
{
// syntax for scaling x and y dimensions. 1 = 100%, .5 = 50%
greenSprite.scaleX = greenSprite.scaleY = .5;
}

Rounded Corners

UNCOMMENTED

var greenShape:Shape = new Shape();
greenShape.graphics.beginFill(0x00FF00);
greenShape.graphics.drawRoundRect(30, 80, 50, 50, 20);
greenShape.graphics.endFill();
var greenSprite:Sprite = new Sprite();
greenSprite.addChild(greenShape);
addChild(greenSprite);

COMMENTED

// draws a rectangle with rounded corners

var greenShape:Shape = new Shape();
greenShape.graphics.beginFill(0x00FF00);
// drawRoundRect(x position, y position, width, height, rounding)
greenShape.graphics.drawRoundRect(30, 80, 50, 50, 20);
greenShape.graphics.endFill();
var greenSprite:Sprite = new Sprite();
greenSprite.addChild(greenShape);
addChild(greenSprite);

Draw Circle

UNCOMMENTED

var myCircle:Shape = new Shape();
myCircle.graphics.beginFill(0x00F0F0);
myCircle.graphics.lineStyle(5, 0×000000);
myCircle.graphics.drawCircle(60, 60, 30);

myCircle.graphics.endFill();

var circleSp:Sprite = new Sprite();
circleSp.addChild(myCircle);
addChild(circleSp);

 

COMMENTED

// dynamically draws circle with outline

var myCircle:Shape = new Shape();
myCircle.graphics.beginFill(0x00F0F0);
// the next two lines must be placed between the beginFill and endFill lines
myCircle.graphics.lineStyle(5, 0×000000);
myCircle.graphics.drawCircle(60, 60, 30);
myCircle.graphics.endFill();

var circleSp:Sprite = new Sprite();
circleSp.addChild(myCircle);
addChild(circleSp);

Hit Test

UNCOMMENTED

var greenShape:Shape = new Shape();
greenShape.graphics.beginFill(0x00FF00);
greenShape.graphics.drawRect(80, 20, 50, 50);
greenShape.graphics.endFill();
var greenSprite:Sprite = new Sprite();
greenSprite.addChild(greenShape);
addChild(greenSprite);

var blueShape:Shape = new Shape();
blueShape.graphics.beginFill(0x000FFF);
blueShape.graphics.drawRect(200, 20, 50, 50);
blueShape.graphics.endFill();
var blueSprite:Sprite = new Sprite();
blueSprite.addChild(blueShape);
addChild(blueSprite);

stage.addEventListener(MouseEvent.CLICK, moveIt)
function moveIt(evt:MouseEvent):void
{ greenSprite.x = greenSprite.x + 10;
if(greenSprite.hitTestObject(blueSprite)) {
blueSprite.alpha = .3;
}
}

COMMENTED

// draws two rectangles and places on stage
var
greenShape:Shape = new Shape();
greenShape.graphics.beginFill(0x00FF00);
greenShape.graphics.drawRect(80, 20, 50, 50);
greenShape.graphics.endFill();
var greenSprite:Sprite = new Sprite();
greenSprite.addChild(greenShape);
addChild(greenSprite);

var blueShape:Shape = new Shape();
blueShape.graphics.beginFill(0x000FFF);
blueShape.graphics.drawRect(200, 20, 50, 50);
blueShape.graphics.endFill();
var blueSprite:Sprite = new Sprite();
blueSprite.addChild(blueShape);
addChild(blueSprite);

/* mouse event that moves the green square towards the
blue square when the stage is clicked */

stage.addEventListener(MouseEvent.CLICK, moveIt)
function moveIt(evt:MouseEvent):void
{ greenSprite.x = greenSprite.x + 10;
/* sets the blue square to detect a hit by the green square
and then change the alpha of the blue square */

if
(greenSprite.hitTestObject(blueSprite)) {
blueSprite.alpha = .3;
}
}

Centering a Registration Point

UNCOMMENTED

var mySquare:Shape = new Shape();
mySquare.graphics.beginFill(0x3366FF);
mySquare.graphics.drawRect(-50, -50, 100, 100);
mySquare.graphics.endFill();

var myHolder:Sprite = new Sprite();
myHolder.addChild(mySquare);

addChild(myHolder);
myHolder.x = stage.stageWidth/2; myHolder.y = stage.stageHeight/2;


COMMENTED

/* sets a square’s (virtual) registration point
to its center */

// creates a 100 pixel blue square
var mySquare:Shape = new Shape();
mySquare.graphics.beginFill(0x3366FF);
/* note that the “-50, -50″ on the next line is one-half of the square’s width
which 
puts its center point at the 0, 0 registration point of the myHolder sprite */
mySquare.graphics.drawRect(-50, -50, 100, 100);
mySquare.graphics.endFill();

// instantiates the holder for the square and adds to stage
var
myHolder:Sprite = new Sprite();
myHolder.addChild(mySquare);
addChild(myHolder);

/* positions the center of mySquare to the center of the stage
myHolder.x = stage.stageWidth/2; myHolder.y = stage.stageHeight/2;

Array – SortOn

UNCOMMENTED

var teamArray:Array = ["OK State", "Alabama", "LSU"];
var rankArray:Array = [2, 3, 1];
var assoArray:Array = new Array();
for(var i:int = 0; i<teamArray.length; i++)
{
assoArray[i] = [teamArray[i], rankArray[i] ];
}
trace(assoArray);

assoArray = assoArray.sort();
trace(assoArray);

assoArray = assoArray.sortOn(assoArray[1]);
trace(assoArray);

COMMENTED

// sorts on the 2nd element of an associative array using sortOn()

var teamArray:Array = ["OK State", "Alabama", "LSU"];
var rankArray:Array = [2, 3, 1];
var assoArray:Array = new Array();
for(var i:int = 0; i<teamArray.length; i++)
{
assoArray[i] = [teamArray[i], rankArray[i]];
}
trace(assoArray);
// raw sort:
// Output: Alabama,3,LSU,1,OK State,2

assoArray = assoArray.sort();
trace(assoArray);
// sorted on team names in assoArray[0] elements
// Output: Alabama,3,LSU,1,OK State,2

assoArray = assoArray.sortOn(assoArray[1]);
trace(assoArray);
// sorted on numbers in assoArray[1] elements
// Output: LSU,1,OK State,2,Alabama,3

Timer – Start/Stop

UNCOMMENTED

var myShape:Shape = new Shape();
myShape.graphics.beginFill(0×333666);
myShape.graphics.drawRect(0, 0, 100, 20);
myShape.graphics.endFill();
var myRect:Sprite = new Sprite();
myRect.addChild(myShape);
addChild(myRect);

var myTimer:Timer = new Timer(100, 40);
myTimer.addEventListener(TimerEvent.TIMER, moveRect);
myTimer.start();

function moveRect(e:TimerEvent):void
{
if(myRect.x > 20){trace(‘Timer stopped’); myTimer.stop();}
myRect.x +=3;
}

COMMENTED

/* moves a rectangle across the stage using a timer
and uses two methods to stop it */

// draws a rectangle and adds to the stage
var myShape:Shape = new Shape();
myShape.graphics.beginFill(0×333666);
myShape.graphics.drawRect(0, 0, 100, 20);
myShape.graphics.endFill();
var myRect:Sprite = new Sprite();
myRect.addChild(myShape);
addChild(myRect);

/* settings for Timer(100, 40) below:
1000 = 1 sec, 100 = 1/10th sec, 10 = 1/100th sec.
40
sets the number of times the timer cycles */
var myTimer:Timer = new Timer(100, 40);
myTimer.addEventListener(TimerEvent.TIMER, moveRect);
myTimer.start();

function moveRect(e:TimerEvent):void
{
/* the if statement manually stops the timer when myRect x exceeds
20 pixels.
Try commenting out the if statement and myRect will move until the timer above
has cycled 40 times, moving further across the stage.

if(myRect.x > 20){trace(‘Timer stopped’); myTimer.stop();}
myRect.x +=3;
}

Set Bounds

UNCOMMENTED

var myShape:Shape = new Shape();
myShape.graphics.beginFill(0×995544);
myShape.graphics.drawRect(0, 0, 50, 50);
myShape.graphics.endFill();
var mySquare:Sprite = new Sprite();
mySquare.addChild(myShape);
addChild(mySquare);

mySquare.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);

var myLimit:Rectangle = new Rectangle(0, 0, stage.stageWidth – mySquare.width,
stage.stageHeight – mySquare.height);

function dragIt(evt:MouseEvent):void
{
mySquare.startDrag(false, myLimit);
}
function dropIt(eut:MouseEvent):void
{
mySquare.stopDrag();
}

COMMENTED

/*  places a draggable square on the stage and limits its movement
so that it stays on the stage */

// draws a square and adds it to the stage
var myShape:Shape = new Shape();
myShape.graphics.beginFill(0×995544);
myShape.graphics.drawRect(0, 0, 50, 50);
myShape.graphics.endFill();
var mySquare:Sprite = new Sprite();
mySquare.addChild(myShape);
addChild(mySquare);

// listeners for dragging and dropping the square
mySquare.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);

/* instantiates a rectangle with an upper left corner set to coordinates 0,0 and a
lower right corner to the stage height & width, minus the square’s width & height */ 

var myLimit:Rectangle = new Rectangle(0, 0, stage.stageWidth – mySquare.width,
stage.stageHeight – mySquare.height);

function dragIt(evt:MouseEvent):void
{
// sets draggable limit of mySquare to the area of the myLimit rectangle
mySquare.startDrag(false, myLimit);
}
function dropIt(eut:MouseEvent):void
{
mySquare.stopDrag();
}

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
*/

Save Text To File

COMMENTED

// presented as commented only

// draws shape for button, adds text to it and adds to stage
var myShape:Shape = new Shape();
myShape.graphics.beginFill(0×669999);
myShape.graphics.drawRect(0, 0, 60, 20);
myShape.graphics.endFill();
var yourButton:Sprite = new Sprite();
yourButton.addChild(myShape);
var myText:TextField = new TextField();
myText.text = “Save File”;
myText.selectable = false;
yourButton.addChild(myText);
addChild(yourButton);

// instantiates input text field to hold typed text and adds to stage
var mytextfield:TextField = new TextField();
mytextfield.type = “input”;
mytextfield.width = 400;
mytextfield.height = 200;
mytextfield.border = true;
mytextfield.wordWrap = true;
mytextfield.y = yourButton.y + 40;
addChild(mytextfield);

// opens file browser and saves text to a file
var myfile:FileReference = new FileReference();

yourButton.addEventListener(MouseEvent.CLICK, savefile);
function savefile(myevent:MouseEvent):void
{
myfile.save(mytextfield.text, “myTextFile”);
// or use this instead to browse to a folder and name the file yourself:
// myfile.save(mytextfield.text, “”);
}
// remember to retype quotation marks if you get a 1093 error