0
votes

I have an actionscript file in my flex project. I loaded the actionscript into flex via addElement()

MXML File:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init(event)">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        public var sv:Myastest;
        protected function init(event:FlexEvent):void
        {
            sv = new Myastest();
            addElement(sv);
            sv.classfunc();
        }

        public function mainfunc():void
        {
            trace("mainfunc called");
        }
    ]]>
</fx:Script>

</s:WindowedApplication>

ActionScript File:

package
{
import flash.events.Event;

import mx.core.UIComponent;

[SWF(frameRate="25", backgroundColor="#000000")]
public class Myastest extends UIComponent
{

    public function Myastest()
    {
        trace("loaded..");
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(event:Event):void
    {
        trace("added to stage");
    }

    public function classfunc():void
    {
        trace("classfunc called");
    }

}
}

How can I call mainfunc() from the actionscript file? Thank you.

2
You shouldn't: that's breaking encapsulation. You should use events to communicate from a child component to its ancestors. - RIAstar

2 Answers

0
votes

IN THE CLASSMANAGEREVENT

public class ManagerEvent 
{

public static const EVENT_MAIN_CLASS_FUNC:String = UIDUtil.createUID();
FlexGlobals.topLevelApplication.addEventListener(EVENT_MAIN_CLASS_FUNC, executeMainClassFuncCommand)

  public function executeMainClassFuncCommand(event:MainClassFunc):void
        {
          var cmd:FUNCTIONCommand = new FUNCTCommand();
          cmd.execute(event);
        }

}

CLASS EVENT

public class MainClassFunc extends Event
    {
        public function MainClassFunc()
        {
            super(ManagerEvent.EVENT_MAIN_CLASS_FUNC);
        }
    }

IN THE CHILD

var mainClassFunc:MainClassFunc = new MainClassFunc();
FlexGlobals.topLevelApplication.dispatchEvent(mainClassFunc);
0
votes

To expand on RIAStar's comment, you could do this:

MXML File:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"     applicationComplete="init(event)">
<fx:Script>
<![CDATA[
    import mx.events.FlexEvent;
    public var sv:Myastest;
    protected function init(event:FlexEvent):void
    {
        sv = new Myastest();
        sv.addEventListener(Myastest.EVENT_NAME, mainfunc);
        addElement(sv);
        sv.classfunc();
    }

    public function mainfunc(e:Event):void
    {
        trace("mainfunc called");
    }
]]>
</fx:Script>

</s:WindowedApplication>

ActionScript File:

package
{
import flash.events.Event;
import mx.core.UIComponent;

[SWF(frameRate="25", backgroundColor="#000000")]
public class Myastest extends UIComponent
{
    public static const EVENT_NAME:String = "EVENT_NAME";
    public function Myastest()
    {
        trace("loaded..");
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(event:Event):void
    {
        trace("added to stage");
    }

    public function classfunc():void
    {
        trace("classfunc called");
        //assuming you want the mainfunc to get called from here
        dispatchEvent(new Event(EVENT_NAME));
    }

}
}

Please note that you need to call removeEventListener before you set sv = null.