0
votes

Okay, I've always just developed my projects in as3 instead of mxml and I usually just setup my application to run a main actionscript file as my main doc root, sort of like how you do it in flash but in flash builder I do something like this:

<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    .....etcetc
>

    <com:MyAS3DocClass>
    </com>

    <fx:Script>
        <![CDATA[
            public function call_from_outside():void
            {
                //Some code stuff here...
            }
        ]]>
    </fx:Script>

</s:Application>

Now say MyAS3DocClass has a public function within it:

public function hitme():void
{
    trace('ouch');
}

My question is, how can I call place a call to that function hitme() within the call_from_outside() function between the <fx:Script> tags?

The reason why I'm doing this is because I have some flash swfs that I load into another swf file and I can access the top level public functions of those flash swfs, however the top level of the flash builder/flex swfs is the main mxml file not my fake as3 class root. Of course I should think there is a way for me to access the as3 class methods of the mxml component but for keeping things with the same structure, I can bypass having to modify my loader scripts. Anyone have an ideas?

1

1 Answers

3
votes

(Please note that I changed your closing tag below. It will cause headaches if you omit that).

It looks like all you need to do is add an ID to your custom class:

<com id="myCustomClass">
    </com:MyAS3DocClass>

And then you can simply access that value as a variable name inside the script tag:

        public function call_from_outside():void
        {
            myCustomClass.hitme();
            //other stuff
        }

AND HERE'S WHY!

When you assign an ID to a tag in an MXML file, it is the same thing as adding public var <your-variable-name-here>:<tag-class> to an AS file (of course that's done by the compiler, so you shouldn't need to care). Once you assign an ID to MyAS3DocClass, it is instantly a publicly accessible variable. Once it is a publicly accessible variable, it can be used in public, protected, internal, namespace, and private functions!

If you don't like this idea and your custom class is a DisplayObject, you can also do this:

<com:MyAS3DocClass name="myCustomClass" />

then, in the script tag:

        public function call_from_outside():void
        {
            MyAS3DocClass(getChildByName("myCustomClass")).hitme();
            //other stuff
        }