4 Replies Latest reply on Jun 8, 2011 10:14 PM by seRajan

    How simulate mouse click on button on loaded swf ? Help please !!!

    Marina Mal

      I have flex 3 application with loaded swf file. How can I simalate mouse click on my loaded swf on x y coordinates ?? On this x y point my loaded swf has a button. I need sumalate to click this button without real mouse click.

      My code :

      var newEv:MouseEvent = new MouseEvent(MouseEvent.CLICK, true, false, x0 , y0) ;

      content.addEventListener(MouseEvent.CLICK, onMouseClick);

      content.dispatchEvent(newEv);

      function onMouseClick executed but my button on swf is not clicked !!!! Thank you very much for any help.

        • 1. Re: How simulate mouse click on button on loaded swf ? Help please !!!
          Flex harUI Adobe Employee

          You have to dispatch it off a reference to the button.

          • 2. Re: How simulate mouse click on button on loaded swf ? Help please !!!
            seRajan Level 1

            I updated sample code for dispatchEvent, I think its will help you.

             

            <?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" minWidth="955" minHeight="600"
                           creationComplete="init();">
                <fx:Script>
                    <![CDATA[
                        import mx.controls.Alert;
                        private function init():void
                        {
                            bindBtn1.addEventListener(MouseEvent.CLICK, onMouseClick);

                            bindBtn2.addEventListener(MouseEvent.CLICK, onCLC);
                        }
                       
                        private function onMouseClick(evt:MouseEvent):void
                        {
                            trace(evt);
                            trace(evt.currentTarget+" : "+evt.type+" : "+evt.localX+" : "+evt.localY);
                           
                            var dEvt:MouseEvent = new MouseEvent(MouseEvent.CLICK, true, false, 20, 30);
                            bindBtn2.dispatchEvent(dEvt);
                        }
                       
                        private function onCLC(evt:MouseEvent):void
                        {
                            trace(evt);
                            trace(evt.currentTarget+" : "+evt.type+" : "+evt.localX+" : "+evt.localY);
                        }
                    ]]>
                </fx:Script>
                <mx:VBox>
                    <mx:Button id="bindBtn1" label="Button 1"/>
                    <mx:Button id="bindBtn2" label="Button 2"/>
                </mx:VBox>
            </s:Application>

             

             

            This application have two button contorl bindBtn1 and bindBtn2.

            Application creation complete, added the btn1 Mouse click event. Once click the button 1, i am going to dispatching the button 2.

             


            Here i know the when should i trigger the dispatchEvent. Once click the button1, we are going to trigger the button2 mouseEvent.

             

            Use as your requirment.

             

            - Rajan

            • 3. Re: How simulate mouse click on button on loaded swf ? Help please !!!
              Marina Mal Level 1

              Thank you , but this example not help me. I don't  know  id of button which I need to simulate clicked. This button is part of loaded swf  and I can see it, I can get coordinates of point on it , but I can't simulate mouse click on it and get action which I can get by real click on this button.

              I use <mx:SWFLoader id="content"  .../>

              Who must dispatch event ??

              content.content.dispatchEvent(newEv);

              content.dispatchEvent(newEv);

              not work......

              • 4. Re: How simulate mouse click on button on loaded swf ? Help please !!!
                seRajan Level 1

                I think you have the button inside the SWF file. But if you want to add the event Listener in flex Application.

                 

                Simple way, you can add the addEventListener inside the Flash file itself.

                inside the that event handler you can dispatch the event.

                 

                like :

                 

                inside the Flash file:

                function buttonMouseClickHandler(evt:MouseEvent):void

                {

                     evt.stopImmediatePropagation();

                 

                     button.dispatchEvent(new Event("buttonClicked", true));

                }

                 

                inside the Flex:

                 

                content.addEventListener("buttonClicked", buttonClickHandler);


                function buttonClickHandler(evt:Event):void

                {

                     evt.stopPropagation();

                 

                     trace(evt.target, evt.currentTarget);

                }