3 Replies Latest reply on Oct 9, 2007 9:22 AM by shahinyan

    addChild question

    shahinyan
      I've face a problem adding on click event custom component to my stage. Imagine a button and a canvas. What i want to do is to add a custom component (which is simple to state thing, one state is button, on hover it shows menu) to my stage.
      What is the logic of doing it? Please help.

      Thanks in advance,
      Mika
        • 1. Re: addChild question
          tagyro
          // let's suppose you have mycomp you're component with the two states
          // you have to do the rollOver/rollOut script in your component
          // in mycomp.mxml
          this.addEventListener(MouseEvent.ROLL_OVER,over);
          this.addEventListener(MouseEvent.ROLL_OUT,out);
          function over ():void {
          //go to state 2
          }
          funciton out():void {
          //go to state 1
          }
          // and in the main.mxml
          var comp:mycomp=new mycomp;
          canvas.addChild(comp);

          // is this what you want to do?
          • 2. Re: addChild question
            AnandGanesan
            Try this

            First Create a component file

            MyCustomComp.mxml
            -------------------------------
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" creationComplete="createAndShow()">
            <mx:Script>
            <![CDATA[
            import mx.controls.Menu;
            private function createAndShow():void
            {
            var myMenu:Menu = Menu.createMenu(null, myMenuData, false);
            myMenu.labelField="@label";
            myMenu.show(10, 10);
            }
            ]]>
            </mx:Script>
            <mx:XML format="e4x" id="myMenuData">
            <root>
            <menuitem label="MenuItem A" >
            <menuitem label="SubMenuItem A-1" enabled="False"/>
            <menuitem label="SubMenuItem A-2"/>
            </menuitem>
            <menuitem label="MenuItem B" type="check" toggled="true"/>
            <menuitem label="MenuItem C" type="check" toggled="false"/>
            <menuitem type="separator"/>
            <menuitem label="MenuItem D" >
            <menuitem label="SubMenuItem D-1" type="radio" groupName="one"/>
            <menuitem label="SubMenuItem D-2" type="radio" groupName="one"
            toggled="true"/>
            <menuitem label="SubMenuItem D-3" type="radio" groupName="one"/>
            </menuitem>
            </root>
            </mx:XML>
            </mx:Canvas>


            Then create a Main File.

            MainApplication.mxml
            ------------------------------

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" backgroundColor="#ffffff" >
            <mx:Script>
            <![CDATA[
            public function createMenu():void
            {
            myCanvas.addChild(new MyCustomComp());

            }
            public function destroyMenu():void
            {
            myCanvas.removeChildAt(0);
            }
            ]]>
            </mx:Script>

            <mx:Canvas id="myCanvas"/>
            <mx:Button id="myButton" rollOver="createMenu()" rollOut="destroyMenu()"
            label="Create Menu Component"/>
            </mx:Application>

            Regards,
            Anand.G

            • 3. Re: addChild question
              shahinyan Level 1
              Thanks a lot!
              Such a help!