After viewing Lee Brimelow’s tutorial in building Custom Flash Panels, I thought I’d take a crack at the Flash JavaScript API because I usually have a pretty standard way of setting up my default movies. This is my first attempt so definitely there’s a lot of room for improvement.
There are currently 2 buttons in the panel - one to setup the movie and the other to insert a distribution licence agreement, which I normally do not put in my code but I thought it’ll be useful for other developers. I should start including this in my codes. ![]()
Setup Movie
This function will create 3 layers on top of the default “Layer 1″:
- Logic (folder)
- Stops (normal)
- Scripts (normal)
Both Stops and Scripts layers will have Logic as their parentLayer property. But before assigning a parentLayer, you should first access which layers you want this to be associated with through the layers property - a property returning an “array of layer objects”. One very important thing that LiveDocs failed to mention is the layers property reads timeline layers from top to bottom - so the top-most layer is index zero. But when you add layers using addNewLayer() method it naturally adds a new layer on top of the current layer. Each new added layer will have an index zero.
Here is where I got really confused. I always thought that when you add an item to a stack in an array, by default it should call Array.push(). But not in this case.
Logic folder layer will be locked and all child layers will inherit this property.
The Setup Movie button also adds folders into the library for better organization of assets. It modifies the stage properties:
- width: 1000
- height: 600
- frameRate: 30
Insert Licence
Most codes I download over the internet have an attached distribution agreement and most of them are very long - occupying several lines. This will cause a problem when you set this long text - with carriage returns and special characters - as a string value to a variable. ActionScript will throw an error. Doug McCune has a neat trick to this problem - encapsulate the text in CDATA sections.
var longText:String = String(<![CDATA[ The quick brown fox jumped over the lazy dog. ]]>);
The 2nd problem. This text still has carriage returns and special characters. Escaping each special character by brute force defeats the purpose of programming. Here’s my little function utilizing RegExp.
function formatJSFL(str:String):String
{
var pattern:RegExp;
var replace:String;
var formattedString:String = str;
// forward slashes
pattern = /\//g;
replace = '\/';
formattedString = formattedString.replace(pattern, replace);
// back slashes
pattern = /\\/g;
replace = '\\\\';
formattedString = formattedString.replace(pattern, replace);
// double quotes
pattern = /\"/g;
replace = '\"';
formattedString = formattedString.replace(pattern, replace);
// single quotes
pattern = /\'/g;
replace = '\\\'';
formattedString = formattedString.replace(pattern, replace);
// asterisks
pattern = /\*/g;
replace = '\\*';
formattedString = formattedString.replace(pattern, replace);
// colons
pattern = /\:/g;
replace = "\\\:";
formattedString = formattedString.replace(pattern, replace);
// carriage return
pattern = /\r/g;
replace = "";
formattedString = formattedString.replace(pattern, replace);
// new line feed
pattern = /\n/g;
replace = "\\n";
formattedString = formattedString.replace(pattern, replace);
return formattedString;
}
And here’s how this text should look like. I got this from one of Grant Skinner’s FLAs. I modified it a bit… (dude, I hope you don’t mind.)
/******************************************************************* * Application Name: * Author: * Date: * Version: * Website: * Email Address: * Country: * Notes: * * * * Copyright (c) [YYYY] [Author Name] * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. ********************************************************************/
The next thing I want to happen is to be able to insert this licence agreement into an ActionScript file. I can’t figure out how to do this.
Installation
Copy and paste the Movie Utilities.swf into C:\Documents and Settings\{user}\Local Settings\Application Data\Adobe\Flash CS3\en\Configuration\WindowSWF.
In your Flash IDE, select Window > Other Panels > Movie Utilities.
Get the source here.
