4 Replies Latest reply on Sep 21, 2010 8:13 AM by djh88ukwb

    Run function in component from Main Application

    djh88ukwb Level 1

      Hello all.

       

      I have my main.mxml application, that has a component inside of it.  The component is called <ns1:record/> with an id of "rec".

       

      This component has a function inside of it named doConnect(event:MouseEvent).

       

      Now i want to be able to add an event listener to a button on the Main.mxml that will run a function inside the component when clicked.

       

      I have managed to add an event listener to the Main.mxml that triggers a function held within the Main.mxml when a button clicked inside the component is clicked.

       

      I did this by using this code.

                  <ns1:record id="rec" x="9" y="6" camera="{camera}" microphone="{microphone}" creationComplete="makeEvent()">
                  </ns1:record>

       

                  //Event listener
                  public function makeEvent():void {
                      rec.backbtn.addEventListener(MouseEvent.CLICK,swapstate);
                  }
                  protected function swapstate(event:MouseEvent):void {
                      viewstack1.selectedChild=config;
                  }

       

      No matter what i try thou i cant get this to work the other way around.

       

      What i need help with is - clicking a button within the Main.mxml to run a function inside the component Record.

       

      If anyone can help that would be great !

       

      Thanks in advance.

        • 1. Re: Run function in component from Main Application
          BhaskerChari Level 4

          Hi djh88ukwb,

           

          From your post if I understand you correctly you want to listen for a event in record component when a button in main mxml is clicked...based on this

           

          assumption I am suggesting you a solution...

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">
          <mx:Script>
            <![CDATA[
            
             private function onButtonClicked(event:MouseEvent):void
                {
             rec.swapstate(event);
                }
            ]]>
          </mx:Script>

          <ns1:record id="rec" x="9" y="6" camera="{camera}" microphone="{microphone}" creationComplete="makeEvent()"></ns1:record>
          <mx:Button id="myBtn" label="Call Function in Record Comp" click="onButtonClicked()" />
          </mx:Application>

           

           

          <!-- In your record component Add a public function  swapstate as shown below-->

           

          public function swapstate(event:MouseEvent):void {
                          viewstack1.selectedChild=config;
          }

           

          Please try this and let me know....

           

          Thanks,

          Bhasker

          • 2. Re: Run function in component from Main Application
            djh88ukwb Level 1

            Hello,

             

            Thanks for the reply and yes that is what i wanted to do.  However i get an error when trying it.

             

            Here is what i had.

             

            Main.mxml

                        private function onButtonClicked(event:MouseEvent):void
                        {
                            viewstack1.selectedChild=record2;
                            rec.MainRun(event);
                        }

             

             

            <s:Button x="552" y="399" label="Button" id="pro" click="onButtonClicked(event)"/>

             

            record / rec component

                        public function MainRun(event:MouseEvent):void {
                            mainInit();
                        }
                       
                        public function mainInit():void
                        {
                            stage.align = "TL";
                            stage.scaleMode = "noScale";
                            videoCamera = new Video(320,240);
                            videoCameraContainer.addChild(videoCamera);
                           
                            connectStr.text = "rtmp://88.208.202.35/live";
                            connectButton.addEventListener(MouseEvent.CLICK, doConnect);
                            doPublish.addEventListener(MouseEvent.CLICK, publish);
                        //    pro.addEventListener(MouseEvent.CLICK, doConnect);
                           
                            enablePlayControls(false);
                           
                            startCamera();
                            doConnect(null);
                        }

             

            I get the following error when i click the button that is supposed to run the function

             

            TypeError: Error #1009: Cannot access a property or method of a null object reference.
                at Main/onButtonClicked()[C:\Users\Catherine Stewart\Adobe Flash Builder 4\Record\src\Main.mxml:98]
                at Main/__pro_click()[C:\Users\Catherine Stewart\Adobe Flash Builder 4\Record\src\Main.mxml:114]

             

            Hope you can help!

            • 3. Re: Run function in component from Main Application
              BhaskerChari Level 4

              I think you have placed the below line fof code in wrong place...

               

              viewstack1.selectedChild=record2;

               

              I think viewstack1 is in the record component but you have used it in the main mxml itself which doesn't exist....so you need to put that line of code in the public funcytion in the record component...and not in main mxml.

               

              Thanks,

              Bhasker

              1 person found this helpful
              • 4. Re: Run function in component from Main Application
                djh88ukwb Level 1

                Ah, no sorry.

                 

                The viewstack code is in the correct place.

                 

                The Main.mxml has a viewstack with two views.

                 

                The component is in the second view.

                 

                So when you click the button, the script should swap you to the second state and then call the function that is located within the component that is now being shown.

                 

                Does this help things?  I hope you understand it a bit better now.

                 

                **** **** Edit **** ****

                 

                I now know what the problem is.

                 

                Because the component is on the second viewstate, i am trying to access a function that the browser hasnt yet seen.  Which is causing the problem.

                 

                If i load up in the viewstate with the component, then go back to the first state and trying using the function all works well, just as i imagined tbh.

                 

                Is there a way to resolve my issue, which is trying to access a function of a component that hasnt yet been seen ?

                 

                Thanks !

                 

                **** **** Edit **** ****

                 

                I have now completed this task thanks !

                 

                All sorted