Ultrashock Tutorials > Flash5 > Undocumented AS Commands  
 
by: Robert Penner, robertpenner.com


 
Undocumented AS Commands
 

For many moons, the secret words that begin with the letters A and S were known only to a small but powerful elite. But there were those whose thirst for the High Knowledge could not be quenched. They came from far away lands, bearing unusual names like Casper and Igor. They searched high and low, and now the forbidden words have been revealed to the partakers of the Wiki.

I will walk through the AS commands and comment on how they are used, what we can learn from them, and whether they have survived into Flash 6.

ASFunction

ASFunction is used to trigger ActionScript functions from a link in an HTML text field. This handy feature didn't make it into the manual that shipped with Flash 5, but it has been added to the first revision of the ActionScript Dictionary.

In this example, an HTML text field already exists on the stage, and its variable is linkVar. The text "Click Here" is a hyperlink inside the text field. The print() function is called when the link is clicked:

function print (text) {
    trace ("text:" + text);
}
linkVar = "<A HREF='asfunction:print,linoleum'>Click Here</A>";

Clicking the hyperlink shows produces text:linoleum in the Output Window.

Note that you can only pass one argument to the function using ASFunction, and the argument must be a string. You can simulate multiple arguments, though, by separating values with a delimiter. For example, you could separate the arguments with | (pipe):

linkVar = "<A HREF='asfunction:print3Args,Hewey|Dewey|Louie'>Click Here</A>";

Then, the function print3Args() splits the string into an array:

function print3Args (text) {
    var argsArray = text.split ("|");
    trace (argsArray[0]); // Hewey
    trace (argsArray[1]); // Dewey
    trace (argsArray[2]); // Louie
}

A final note: test ASFunction code carefully, in multiple browsers. Using certain ActionScript functions like gotoAndStop() can cause crashes. Generally, it's best to create your own custom functions to use with ASFunction.

ASClamp()

The ASClamp() function forces a number to fall within a given range. This is how it is internally defined in the Flash 5 player:

function ASClamp (v, mn, mx) {
    if (isNaN(v)) {
        v = 0;
    }
    return Math.max( mn, Math.min(number (v), mx) );
}

First, the value v is checked to make sure it is a number. Then, Math.max() and Math.min() are used to force v between the boundaries mn (minimum number) and mx (maximum number). For instance, if v is -2, mn is 10, and mx is 20, ASClamp() will return 10:

trace ( ASClamp (-2, 10, 20) ); // output: 10

Similarly, a higher number than 20 will be clamped to 20:

trace ( ASClamp (33, 10, 20) ); // output: 20

ASClamp() is absent from the Flash 6 player. However, it is a useful function that you may consider adding to your library--perhaps under another name, like Math.clamp().

ASClamp2()

Like ASClamp(), ASClamp2() forces a number to fall within a given range. This is its code in the Flash 5 player:

function ASClamp2 (v, mn, mx) {
    v = Number (v);
    return ASClamp (v < 0 ? mx + v : v, mn, mx);
}

The difference between the two is that ASClamp2() changes a negative v to a positive value before calling ASClamp(). For instance, if v is -2, mn is 10, and mx is 20, ASClamp() will add 20 to make it 18:

trace ( ASClamp2 (-2, 10, 20) ); // output: 18

However, a higher number than 20 will be clamped to 20, just like ASClamp():

trace ( ASClamp (33, 10, 20) ); // output: 20

ASClamp2() is absent from the Flash 6 player, but the function's code may be otherwise informative or useful.

ASInstanceOf()

The ASInstanceOf() function checks if an object is an instance of a particular class. This is its code in the Flash 5 player:

function ASInstanceOf (o, p) {
    if (typeof (o) == "object") {
        while ((o = o.__proto__) != null) {
            if (o == p) {
                return true;
            }
        }
    }
    return false;
}

The object o has its inheritance chain is checked against a supplied constructor prototype p. If o is an instance of the corresponding class (or a subclass thereof), the value true is returned; otherwise, false.

ASInstanceOf() is absent from the Flash 6 player, but you may find the function useful for checking the prototype chains of your own objects.

ASNew()

The ASNew() function is able to tell whether or not the currently executing function was called with the "new" operator. It returns true if this is the case, false otherwise. For instance, the Number() and String() functions can be used either as constructors or datatype conversion functions. Internally, these functions call ASNew() to check if "new" was used, and decide how to behave accordingly.

Unfortunately, ASNew() is absent from the Flash 6 player.

ASNative()

In general, ASNative (i, j) returns a function reference. It's like all the Flash functions are stored in a spreadsheet, and you can access them by rows and columns with ASNative(). A convenient way to work with ASNative functions is to assign the result to a variable, and then execute the variable as a function. For instance, ASNative (200, 11) returns the Math.random() function. You can retrieve this function using ASNative() and execute it like this:

func = ASNative (200, 11);
trace ( func() ); // output: a random # between 0 and 1

There is no real benefit to running Math.random() this way; functions pulled from ASNative() don't run any faster than normal.

ASNative() has been kept in the Flash 6 player. There is a table of known ASNative() functions here, but some of them may be different in Flash 6.

ASSetNative()

When the Flash Player starts up, it performs an internal initialization before executing the first frame. As part of this process, the Player grabs dozens of functions from the ASNative() table and stores them in various built-in functions and objects.

The ASSetNative() function is used as a shortcut to add a series of ASNative() functions to an object. This is how ASSetNative() is defined in the Flash 5 Player at startup:

function ASSetNative (o, maj, n, min) {
    var i = 0;
    while (i < n.length) {
        o[n[i]] = ASnative(maj, min + i);
        i++;
    }
}

Here is an example of how ASSetNative() is used to define the methods for the Color object:

var o = Color.prototype;
ASSetNative (o, 700, ["setRGB", "setTransform", "getRGB", "getTransform"]);

ASSetNative() has been kept in the Flash 6 player.

ASSetPropFlags()

It's often useful to add a method to the prototype of a built-in class. For example, the following code adds a flipX() method to MovieClip:

MovieClip.prototype.flipX = function () {
    _x = -_x;
};

This is a convenient method, but it has this side effect: flipX will show up as a property in for..in loops:

// in an empty movie clip
for (var i in this) trace (i);
// output: flipX

This result has been called "pollution" by some ActionScripters, and is often undesirable. The worst case is when you add a property to Object.prototype. This causes every object to be "polluted" with that property. This can mess up XML objects in particular, because the inherited property can look like an extra node.

However, the little-known ASSetPropFlags() function comes to the rescue here. The Flash Player uses it secretly to hide properties of built-in objects from for..in loops.

The usage of ASSetPropFlags() is a bit complex. It has many options, which are detailed here. Here is an example of how to use ASSetPropFlags() to hide our flipX() method, defined earlier:

ASSetPropFlags (MovieClip.prototype, ["flipX"], 1);

The first parameter of ASSetPropFlags() is the object in question. The second parameter is an array containing the names of the properties you want to hide. The third parameter is a number telling ASSetPropFlags() which "PropFlags" (property flags) to change. The most common value is 1, which hides properties from for..in loops. With different numbers, you can also prevent properties from being modified or deleted; details are here.

ASNative() is operational in the Flash 6 player. However, you cannot "unhide" properties as you can in Flash 5.

If you want to see more of the AS commands in action, check out this thread, which discusses the setup and structure of Flash's internal objects.

Many thanks to Casper Schuirink and Igor Korgan for their investigative work, and the other Flashcoders Wiki contributers to the undocumented features page.

 
©2002 Ultrashock.com, Inc. - All rights reserved