Search code examples
actionscript-3flashapache-flexflex4.5flashbuilder4

Access currentState from other files?


NOTE: This is not a copy of this question as the answer did not help me.

I have my main file (Main.mxml), and I have a main AS file (main.as). main.as is included by Main.mxml via <fx:Script source="main.as"/>. In main.as, I want to change the currentState of Main.mxml. How would I go about doing this?

Things I have already tried:

  • this.parent.currentState = "c_main";
  • this.parentDocument.currentState = "c_main";
  • this.parentApplication.currentState = "c_main";
  • The answer in this question.

Solution

  • Is this what you're trying to do?


    main.mxml:

    <?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"
                           addedToStage="_onStaged(event)"
                           stateChangeComplete="_stateChangeCompleteHandler(event)">
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
    
        <s:states>
            <s:State name="c_main"/>
        </s:states>
    
        <fx:Script source="main.as" />
    </s:WindowedApplication>
    

    main.as:

    import flash.events.Event;
    import mx.events.FlexEvent;
    
    
    // ActionScript file
    private function _onStaged(event:Event = null):void
    {
        this.currentState = "c_main";
    }
    
    protected function _stateChangeCompleteHandler(event:FlexEvent):void
    {
        trace("the state was set to "+this.currentState);
    }