6 Replies Latest reply on Aug 28, 2009 2:21 PM by DeyBwah

    Newbie Question (syntax related)

    DeyBwah

      What is the syntax to target a function that exists inside of a different component?

       

      I'm trying to get a button within component A to target and run a function within component B.

       

      The function that I'm trying to target executes actions that affect only that component it resides in.

       

      So basically, I want to trigger a function within a component, from a different component.

       

      ------------------------------------------------------------------------------------

       

      "Component A"

      <mx:Button id="backToMovieBtn" label="Back to Movie" click="???" />

       

       

       

      "Component B"

      private function showVideo():void {

           this.visible = true;

           this.includeInLayout = true;

      }

       

      Thanks everyone!

      DK

        • 1. Re: Newbie Question (syntax related)
          Gregory Lafrance Level 6

          "Component A"

          <mx:Button id="backToMovieBtn" label="Back to Movie" click="compB.showVideo()" />

           

           

           

          "Component B"  where its id is "compB"

          public function showVideo():void {

              this.visible = true;

              this.includeInLayout = true;

          }

           

           

          notice showVideo is now public.

           

          If this post answered your question or helped, please mark it as such.

          1 person found this helpful
          • 2. Re: Newbie Question (syntax related)
            DeyBwah Level 1

            Hi Greg!

             

            I got an error stating "Access of undefined property "compB".

             

            I forgot to mention a key point that both of those components are being pulled into the Main Application via addChild so there is no direct connection between the components beyond them sharing the same Main Application field... Is it still possible to run that function in CompB from CompA via the instances that exist in the Main Application?

             

            I'm only about 1 month into Flex Development as well as OOP principles, thank you for the quick response!!!

            DK

            • 3. Re: Newbie Question (syntax related)
              Gregory Lafrance Level 6

              You should have a variable name for "compB" that you use in the addChild() method. Replace my compB with that variable name.

               

              If you are using addChild(new myFancyCompB()), do this instead:

               

              var myB:myFancyCompB = new myFancyCompB();

              addChild(myB)

               

              Then you will have access to the variable name.

               

              If this post answered your question or helped, please mark it as such.

              1 person found this helpful
              • 4. Re: Newbie Question (syntax related)
                DeyBwah Level 1

                Thanks again for the quick response,

                 

                Here is the code I have in my main application...

                 

                 

                Main Application

                import com.compA;

                import com.compB;

                 

                private var fancyCompA:CompA

                private var fancyCompB:CompB

                 

                private function init():void {

                     fancyCompA = new CompA();

                     fancyCompB = new CompB();

                 

                     addChild(fancyCompA);

                     addChild(fancyCompB);

                }

                 

                Code for my components...

                 

                compA

                <mx:Button id="backToMovieBtn" label="Back to Movie" click="fancyCompB.showVideo()" />

                 

                compB

                 

                public function showVideo():void {

                    this.visible = true;

                    this.includeInLayout = true;

                }

                 

                 

                I still receive the error stating "Access of undefined property compB"

                 

                Beyond just the straight code to get this working, my bigger question is...

                 

                Is it "possible" to access functions component to component with no "direct" relationship to one another beyond sharing the same Main Application field?

                 

                If it is, what's the syntax to accomplish this?

                 

                Jumping into AS3 has been quite overwhelming to say the least, especially coming from a background of zero OOP experience. :/

                 

                Thank you!

                DK

                • 5. Re: Newbie Question (syntax related)
                  Andrew Rosewarn Level 3

                  Heres an example that works if this helps

                   

                  Comp A

                   

                  <?xml version="1.0" encoding="utf-8"?>

                  <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="158" height="58" backgroundColor="#FFFFFF">

                  <mx:Button x="21" y="10" label="Button" click="{dispatchEvent(new Event('clicked'))}"/>

                  </mx:Canvas>

                  Comp B

                  <?xml version="1.0" encoding="utf-8"?>

                  <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" backgroundColor="#F5F5F5">

                  <mx:Script>

                  <![CDATA[

                  import mx.controls.Alert;

                   

                  public function sayHello():void {

                  Alert.show("Hello there")

                  }

                  ]]>

                  </mx:Script>

                  </mx:Canvas>

                  Application

                  <?xml version="1.0" encoding="utf-8"?>

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

                  <mx:Script>

                  <![CDATA[

                  private var compA:CompA = new CompA();

                  private var compB:CompB = new CompB();

                   

                  private function addComps():void {

                  compA.x = 100; compA.y = 100;

                  compA.addEventListener('clicked', handleClick);

                  compB.x = 300; compB.y = 300;

                  addChild(compA);

                  addChild(compB);

                   

                  }

                   

                  private function handleClick(event:Event):void {

                  compB.sayHello();

                  }

                  ]]>

                  </mx:Script>

                   

                  </mx:Application>

                  • 6. Re: Newbie Question (syntax related)
                    DeyBwah Level 1

                    Thanks Andrew!!

                     

                    That was EXACTLY what I needed!

                     

                    I can't explain how thrilled I am, not only with the result of a working app, but also learning a new tool that I will never forget..

                     

                    My gratitude goes to you, post haste!

                     

                    Namaste,

                    Dey