Code Samples
All of the classes in the Transform SWF framework which accepts actions, FSDoAction, FSDefineButton, etc. have methods that allow the compiled scripts to be easily added to a movie.
String script = "a = b + c;" int swfVersion = 5; ASParser parser = new ASParser(); ASNode root = parser.parse(script); byte[] compiledScript = root.encode(swfVersion); FSDoAction frameActions = new FSDoAction(compiledScript); movie.add(frameActions);
Compiling scripts stored in files is easily performed using the ActionScript directive #include. For example the file, script.as, contains the following ActionScript statements.
onClipEvent(mouseDown)
{
startDrag();
}
onClipEvent(mouseUp)
{
stopDrag();
}
#include directives may nested to any depth allowing libraries of scripts to be created.
String script = "#include script.as"; ASParser parser = new ASParser(); ASNode root = parser.parse(script); byte[] compiledScript = root.encode(swfVersion);
Scripts can also be created by building trees of ASNodes, providing an alternative to dynamically generating scripts compared to processing strings.
ASNode a = new ASNode(ASNode.Identifier, "a"); ASNode b = new ASNode(ASNode.Identifier, "b"); ASNode c = new ASNode(ASNode.Identifier, "c"); ASNode add = new ASNode(ASNode.Add, a, b); ASNode assign = new ASNode(a, add);
All of these examples are trivial to illustrate how the parser is used to compile scripts and add them to a movie. ActionScript is a fully featured programming language supporting classes and inheritance so extremely complex behaviours can be created for movies - allowing full featured applications to be created using Flash.