Creating a JSFL library... Like FileSystem (by Guy Watson)

94 views
Skip to first unread message

Quentin

unread,
Apr 3, 2006, 8:58:02 AM4/3/06
to flash.jsfl
Hi there readers,
I'd like to create a JSFL library that would contain the functions I
frequently use and this library would be packed as an MXP... Just like
Guy Watson did with his FileSystem extension, except that I only want
JSFL functions in this, no DLL...

Anyone out there has an idea of how it was done ?
I read the MXI file in the FileSystem MXP but couldn't get any clue...

dennisobrien

unread,
Apr 10, 2006, 9:37:59 PM4/10/06
to flash.jsfl
Hi Quentin,

You can use the flash.runScript method to include another jsfl script
as a library. Unfortunately, there is no #include directive, but
runScript can work if you take care to declare your functions as
functionName = function(...) {...}
instead of
function functionName(...) {...}

Here's an example. The two files are library.jsfl which contains all
of your functions you want to reuse, and myCommand.jsfl which is some
script that will use the functions in library.jsfl. The files are in
the same directory.

// library.jsfl
sayHello = function()
{
fl.trace("Hello World.");
}
// end of library.jsfl

// myCommand.jsfl
fl.runScript("file:///library.jsfl");
sayHello();
// end of myCommand.jsfl

Notice that the URI is a relative path. You could also say
fl.runScript("file:///../../shared/library.jsfl");
or the more standard absolute path
fl.runScript"file:///D|/dev/jsfl/shared/library.jsfl");

I have also used shared jsfl scripts to extend some base objects. For
example, to add some useful functions to the String object, you could
make a file String_extend.jsfl

// String_extend.jsfl
String.prototype.lstrip = function()
{
for (var i = 0; i < this.length; i++)
{
if (this.charCodeAt (i) > 32)
{
return this.substr (i, this.length);
}
}
return this;
}

String.prototype.rstrip = function()
{
for (var i = this.length; i > 0; i--)
{
if (this.charCodeAt (i) > 32)
{
return this.substring (0, i + 1);
}
}
return this;
}

String.prototype.strip = function()
{
var s = this.lstrip();
return s.rstrip();
}
// end String_extend.jsfl

// myCommand.jsfl
fl.runScript("file:///String_extend.jsfl");
s1 = " This is a test ";
fl.trace(s1);
fl.trace(s1.length);

s2 = s1.strip();
fl.trace(s2);
fl.trace(s2.length);
// end myCommand.jsfl

It's interesting to note that the Flash Javascript interpreter seems to
keep variables persistent for as long as Flash is running. That means
you could run some command at initialization that defines all the
functions in your library and extends any objects you need extending,
then all subsequent JSFL scripts will have access to these methods even
without explicitly including them with the fl.runScript method. (Try
it yourself by running the scripts above, then commenting out
fl.runScript(...), just to make sure I'm not crazy.) This could be
dangerous if you have a command that overrides a standard method.
Subsequent calls to this method will be altered until Flash is
restarted.

I hope this helps.

--Dennis

Quentin

unread,
Apr 11, 2006, 4:23:32 AM4/11/06
to flash.jsfl
Thanks a lot man for this answer !
In fact I knew about the fl.runScript solution and my question was
really about creating a new object that would be available from
anywhere in any JSFL script, even without any runScript call...
However, I agree with you about the fact that Flash rememberance of the
variables defined in a "Flash session" could be very powerful, but to
me, it has been quite annoying sometimes because some objects or
variables were remaining and I couldn't find a way to delete them...
Changing a Class' prototype could be very helpful too !

Cheers !

Reply all
Reply to author
Forward
0 new messages