0

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.
Community
  • 1
  • 1
cbroughton
  • 1,726
  • 1
  • 12
  • 19
  • Duplicate of [Access "currentState" from other classes?](http://stackoverflow.com/questions/2779334/access-currentstate-from-other-classes). cbroughton, if none of the answers on that question helped you, how about putting a bounty of that question? That'll get more eyes in front of it. – Michael Petrotta May 14 '11 at 00:02
  • This is not the same problem. I noted that in the FIRST LINE of my question. Thanks for reading :P. – cbroughton May 21 '11 at 21:50
  • Problem: This is a different question altogether, hence why I still posted this. – cbroughton May 24 '11 at 07:01

3 Answers3

1

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);
}
duggulous
  • 2,427
  • 3
  • 23
  • 40
  • Kind of, but doing such does nothing. As "this" is main.as, not main.mxml :( It throws no error, but the state is not changed. – cbroughton May 16 '11 at 22:32
  • so you're saying when you run the above code, the _stateChangeCompleteHandler doesn't get invoked? I don't see how "this" can refer to Main.as, since Main.as is not a class definition. The functions defined in Main.as are members of the WindowedApplication defined in Main.mxml What is it that makes you say the state is not changed? How are you verifying that? – duggulous May 16 '11 at 22:43
0

The solution is to bind your application's currentState to a variable. See below for an example/proof-of-concept (tested and working in Flex 4.6):

Main.as:

[Bindable]
public var curState:String = "c_main";

protected function swapState():void
{
if(curState=="c_main")
    curState="c_main2";
else
    curState="c_main";
} 

Main.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx"
           currentState="{curState}">   
  <s:states>
    <s:State name="c_main"/>
    <s:State name="c_main2"/>
  </s:states>

  <fx:Script source="Main.as"/>
  <s:VGroup>
 <s:Label text="1" includeIn="c_main" />
 <s:Label text="2" includeIn="c_main2" />
 <s:Button label="switch" click="swapState()"/>
  </s:VGroup>
</s:Application>
ffxtian
  • 559
  • 2
  • 5
-1

instead of

this.currentState = "c_main"; 

just use

currentState = "c_main";

it works fine!

agf
  • 171,228
  • 44
  • 289
  • 238
Yaffar
  • 1
  • 1
    While you would think this does, it does not. The Flex4.5 IDE (Flash Builder 4.5.1) does not allow you to compile this, EVEN THOUGH the code WOULD work at run-time. Blame Adobe. – cbroughton Nov 14 '11 at 00:44