Ultrashock Tutorials > Flash 8 > Alt Captions  
 
by Julian Wilson, Neverrain.net
Download Source Files  
 
Alt Captions
 
 Introduction: Alt Captions
 Step 01: Getting started
 Step 02: Caption Class
 Step 03: Finishing up
- discuss this tutorial -

Library Class

This class is used to fix object scope using the apply function. Its functionality is the same as Macromedia’s Delegate Class except it enables you to pass parameters to the callback method. For example, say your trying to access a property called “score” inside an onEnterFrame event in a class, without either delegating or storing a local variable to path with you wouldn’t be able to access the property.

Valid - Delegate:

private function phone():Void
{
            var call = false;
            office.phone.onEnterFrame = Delegate(this, “pickup”);
}
private function pickUp():Void
{
            trace(call); //output false
}

Valid, but repetitious – Local Property:

private function phone():Void
{
            var call = false;
            var local = this;
            office.phone.onEnterFrame = function()
{
            trace(local.call); //output false
}

Invalid

private function phone():Void
{
            var call = false;
            office.phone.onEnterFrame = function()
{
            trace(call); //output undefined
}

For these reasons this class has been made to fix the problem.

class Library
{
            static function delegate(object:Object, methodName:String):Function
            {
                        var method = object[methodName];
                        delete methodName;
                        var parameters = arguments.slice(2);

                        return function ()
                        {
                                    //Returns callback and function parameters using the correct scope.
                                    return method.apply(object, parameters.concat(arguments));
                        }
            }
}

Save this file in the base directory as “Library.as”.

Conclusion

Over the process of this article you have learned how to setup class paths, create instances of classes, define class properties, setup a constructor method, implement methods, and fix scope issues. Hopefully you not only better understand the theory behind this technique, but also more about coding with OOP.

Demo:


- discuss this tutorial -
 
©2006 Ultrashock.com - All rights reserved