Question

[NOTE: Right off the bat, I already know the popular theory about "no code on MovieClips," so please don't reply with that (or downvote because of it). Every project is different. I just need the answer.]

I have a project in Adobe Flash CS5.5, Adobe AIR 3, and ActionScript 3.

I need to call a function on the main timeline of the project that the document class is linked to from inside the document class. For the sake of example, let's call this function "Ring()".

How do I call this function, "Ring()" from inside my document class?

Was it helpful?

Solution

Well, since you seem to be using some oldskool ninja techniques, I would suggest that you should keep it simple and straightforward.

Say you have some functions on the main timeline:

function Ring1():String
{
    return "Ring1() called!";
}
var Ring2:Function = function () : String
{
    return "Ring2() called!";
};

The scenario for a document class of the said timeline would be like this:

package  {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.utils.getQualifiedClassName;
    import flash.utils.describeType;

    public class Test extends MovieClip 
    {
        public function Test() 
        {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        }
        private function onMouseDown(event:MouseEvent):void
        {
            trace(getQualifiedClassName(this)+".onMouseDown()");
            try {
                var ring1:Function = this["Ring1"] as Function;
                var ring2:Function = this["Ring2"] as Function;
            } catch (error:Error) {         
                // ignore
            }
            if (ring1 != null) {
                trace("\t", "Ring1", "=", ring1);
                trace("\t", ring1());
            } else {
                trace("\t", "Ring1() function not found in "+this+"!");
            }           
            if (ring2 != null) {
                trace("\t", "Ring2", "=", ring2);
                trace("\t", ring2());
            } else {
                trace("\t", "Ring2() function not found in "+this+"!");
            }

            // for your interest:
            var doc:XML = describeType(this);
            var ring1Node:XML = doc.descendants("method").(@name == "Ring1")[0];
            var ring2Node:XML = doc.descendants("variable").(@name == "Ring2")[0];
            trace("declaration of Ring1:", ring1Node.toXMLString());
            trace("declaration of Ring2:", ring2Node.toXMLString());
            // so, you may probably make use of reflection
            // unless you need to reference dynamic members on the main timeline
        }   
    }
}

See comments in the code above.

OTHER TIPS

Put the function you want to call in your document class, and dispatch a custom event (or any event, if the code is readable) from the timeline of the object and listen for that event on your document class.

So the code breakdown would look like this:

On some frame of the timeline in your document (should work on any object):

var customEvent:Event = new Event(Event.COMPLETE);
this.dispatchEvent(customEvent);

In your document class:

public function DocumentClass()
{
    // get the reference to the object
    lolcats.objectThatDispatchesEvent.addEventListener(Event.COMPLETE, _customHandler);
}

protected function _customHandler(event:Event):void
{
    // FUNCTION NAMES SHOULD START WITH LOWERCASE! ^_^
    Ring();
}

http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html

Basically you register any string that defines your event, Event.COMPLETE evaluates to "complete", you can just register whatever you want, like:

var custEvent = new Event("anyCustomString");
this.dispatchEvent(custEvent);

// catch it with
addEventListener("anyCustomString", _handler);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top