9 Replies Latest reply on Oct 26, 2006 7:11 PM by ForsakenBlade

    Function Not working

    ForsakenBlade Level 1
      Ok my function is not working im not sure why it gives no errors I just want it to show a custom component upon completion

      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" creationComplete="changePage()">

      <mx:Script>
      <![CDATA[
      [Bindable]
      public var loginStatus:int;



      private function changePage():void{
      if(loginStatus == 0){
      <local:login/>
      } else {
      <local:environment/>
      }
      }
      ]]>
      </mx:Script>

      </mx:Application>
        • 1. Re: Function Not working
          ntsiii Level 3
          You can't mix mxml and actionscript that way.

          You need to use the addChild() method of a container.

          Tracy
          • 2. Re: Function Not working
            ForsakenBlade Level 1
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">

            <mx:Script>
            <![CDATA[
            [Bindable]
            public var page1:* = "<local:environment/>";

            private function changePage():void{

            page.rawChildren.addChild( page1 );


            }
            ]]>
            </mx:Script>
            </mx:Application>
            I keep getting an error saying

            Type Coercion failed: cannot convert "<local:environment/>" to flash.display.DisplayObject.
            • 3. Re: Function Not working
              ForsakenBlade Level 1
              I have been trying since yesterday night (lol) and I still haven't been able to figure it out I've tried righting the code several ways too and I still pretty much dont know how to use the addChild method I even checked other sites. I'm just to noob so if anyone has some time can they explain how it works and what I'm doing wrong? Thank you.
              • 4. Re: Function Not working
                rmarp Level 1
                I think you'd want to do something like this:

                <mx:ViewStack selectedIndex="{loginStatus}">
                <local:login/>
                <local:environment/>
                </mx:ViewStack>

                What happens is you are defining both the login and environment components but using the ViewStack component to manage which one is shown at any given time. The selectedIndex property of the ViewStack is bound to your loginStatus variable so all you need to do to change screens is set loginStatus to 1. The databinding mechanism wil pick up the change and show the environment component.

                You may also want to consider moving the loginStatus out to a seperate class like in a model/view/controller pattern.

                Ryan
                • 5. Re: Function Not working
                  ForsakenBlade Level 1
                  alright well i have this now,

                  main.mxml
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">

                  <mx:Script source="functions.as"/>



                  <mx:Label text="Label: {loginStatus}"/>
                  <mx:ViewStack selectedIndex="{loginStatus}" width="100%" x="0" y="0">
                  <local:login/>
                  <local:environment/>
                  </mx:ViewStack>


                  </mx:Application>


                  functions.as
                  [Bindable]
                  public var tileData:Object;

                  [Bindable]
                  public var loginData:Object;

                  [Bindable]
                  public var loginStatus:*;


                  private function resultHandler(event:ResultEvent):void {
                  tileData = event.result.tiles.tile;
                  }

                  public function loginHandler(event:ResultEvent):void {
                  loginData = event.result.Character.CharacterInfo;

                  loginStatus = event.result.Status.loggedIn;
                  }

                  then theres also the Login.mxml component, everything works fine in there the loginStatus variable displays fine but then in the main.mxml if i try to call the loginStatus it doesnt show and i have to have the var set to * or else it doesnt let me put it inside of the selectedIndex because otherwise it would need to be a object.
                  • 6. Re: Function Not working
                    rmarp Level 1
                    The selectedIndex property of the ViewStack is a number, so I would make your loginStatus variable a number. Also, you'll need to initialize it to 0 so the first component inside your viewstack is shown.
                    • 7. Re: Function Not working
                      ForsakenBlade Level 1
                      well the problem isnt the viewstack selectedIndex, its that once logged in the variable never changes because that process is done inside of a custom component and the variable isnt being passed from the custom component to the main application.
                      • 8. Re: Function Not working
                        rmarp Level 1
                        Yeah that's why I was saying you should create a seperate model class as a singleton and have the loginStatus variable off there.
                        • 9. Re: Function Not working
                          ForsakenBlade Level 1
                          Sorry I'm newbie to Flex only been doing it for about 5 hours total (completed the lynda tutorials atleast lol) and have no clue what that means, I do appreciate the help though. So can you help me out here in terms where I could possibly understand?