Sequence v1.02


Download the source files
(Descriptions included in the source .as files)
(Usage example included in Sequence.fla)


Sequence class creates a Sequence object which can execute a series of commands in specified order with one action.
The purpose of this class is to simplify coding with onEnterFrame or setInterval involved.

Simplest usage example would look like this.

seq1 = new Sequence(
      [0, function() {trace("hi")}],
      [5, function() {trace("bye")}]
);
seq1.execute();

This traces 'hi' and then traces 'bye' 5 frames later. Since 'setInterval' is used in this Sequence class, it's actually not '5 frames' but the interval value multiplied by 5. The interval value is set to 80 milliseconds by default. It's, therefore, 400 milliseconds to be exact.
We'll call the number a 'tick' instead of a 'frame' in this Sequence class.

If it's one-time performance, it can also be written the following way.

Sequence(
      [0, function() {trace("hi")}],
      [5, function() {trace("bye")}]
);

This executes the passed commands on the fly without creating a Sequence object.

There are 2 ways to specify a command. One is just to pass a function as seen in the above examples. And the other would look like this.

Sequence(
      [10, myObj, "myMethod", arg1, arg2, ....]
);

This assumes 'myObj' has a method called 'myMethod'. What will happen with this is:

myObj.myMethod(arg1, arg2, ....);

gets executed 10 ticks later.

Another fun with this Sequence class is that you can chain multiple commands by putting them at the same tick, and adding a function call:

Command.callback();

at the end of each method definition. (See "The code for btn1" in the example below)

Also you can nest Sequence objects inside another. (See "The code for btn2" in the example below)

 


Usage Example


 

The code for btn1

#include "Command.as"
#include "Sequence.as"
/*
A Sequence object internally creates command objects which will be executed
at specified 'tickCount's.
If there are multiple commands at the same tickCount, they are chained so each
command will be executed by the callback of the previous command.
*/ //user-function example, used by a movieclip 'window' and a dynamic textfield 'window.msg_txt'.
Object.prototype.linearTween = function (propName, valStep, endVal) {
var callback = Command.callback;
var o = this;
var isDone = (valStep > 0) ? function() {return o[propName] > endVal}
: function() {return o[propName] < endVal};
this.id = setInterval(function () {
o[propName] += valStep;
if (isDone()) {
clearInterval(o.id);
o[propName] = endVal;
callback();
}
}, 50);
};
ASSetPropFlags(Object.prototype, null, 1);
window._xscale = 0;
window._yscale = 10;
//create a Sequence object. Commands on the same tickCount get chained,
//so they will be executed one after another
seq1 = new Sequence(
[ 0, function() {msg_txt.text = ""; Command.callback();}],
[ 0, function() {btn1.enabled = btn2.enabled = false; Command.callback();}],
[ 0, window, "linearTween", "_xscale", 20, 100],
[ 0, window, "linearTween", "_yscale", 20, 100],
[ 5, function() {window.msg_txt.text = "Hello"; Command.callback();}],
[15, function() {window.msg_txt.text = "Bye"; Command.callback();}],
[15, window.msg_txt, "linearTween", "_alpha", -12, 0],
[15, function() {window.msg_txt.text = ""; window.msg_txt._alpha = 100; Command.callback();}],
[20, window, "linearTween", "_yscale", -20, 10],
[20, window, "linearTween", "_xscale", -20, 0],
[20, function() {btn1.enabled = btn2.enabled = true; Command.callback();}],
["onSequenceComplete", function() {msg_txt.text = "Done";}]
);
seq1.setIntervalTime(50);
btn1.onRelease = function () {
seq1.execute();
};
The code for btn2
//Nested sequences example.
seq2 = new Sequence(
[0, seq1, "execute"],
[0, seq1, "execute"],
["onSequenceComplete", function() {msg_txt.text = "All Done";}]
);
btn2.onRelease = function () {
seq2.execute();
};


Back to Flash Home

Copyright©2000-2002 STUDIO FIRST RAYS. All rights reserved