9 Replies Latest reply: Feb 6, 2012 11:12 AM by igregurec RSS

    TOUCH_BEGIN calls function twice

    igregurec Community Member

      hi there people!

      I've decided to experiment with multitouch basics:

       

      import flash.display.MovieClip;

      import flash.events.TouchEvent;

      import flash.events.Event;

      import flash.ui.Multitouch;

      import flash.ui.MultitouchInputMode;

       

      var i:int=0;

      Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

       

      function touchBegin(evt:TouchEvent)

      {

          i++;

          trace(i);

      }

      stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBegin);

       

       

      function touchEnd(evt:TouchEvent)

      {

          i--;

      }

       

      stage.addEventListener(TouchEvent.TOUCH_END, touchEnd);

       

      the results are strange - on every touch_begin event touchBegin() is called twice and on touch_end only once so "i" is getting bigger and bigger...

      any idea?


        • 1. Re: TOUCH_BEGIN calls function twice
          Anton Azarov Community Member

          function touchEnd(evt:TouchEvent)

          {

              i--;

          trace(i);

          }

           

          may be this help?

           

          but anywhere - you should 'save' your touchpoint id and if it wass released - just for decrease for it.

          • 2. Re: TOUCH_BEGIN calls function twice
            boat5 Community Member

            looking at your code, nothing is wrong, bug perhaps? what is your device? i have never seen that happen before

            • 3. Re: TOUCH_BEGIN calls function twice
              boat5 Community Member

              and which SDK?

              • 4. Re: TOUCH_BEGIN calls function twice
                Anton Azarov Community Member

                in fact - this sometimes occur on iPhone 4 and iPhone 3GS with any Air SDK. Simply I don't have another devices for test. But ! This happen when you have more than 2 touch point id at once. But this simply can be solved - use saving of current pressed touchpoint and chen when you doing decrement so if your exact touch_begin ID point was touch_end - than you will do i--. In another case do nothing =)

                 

                This will help you at 100% I done this many times

                • 5. Re: TOUCH_BEGIN calls function twice
                  igregurec Community Member

                  it happened every time in device central and sometimes, with faster taping on iPhone 3GS. I'm planning to create some kind of polyphonic synth - you can imagine what kind of chaos would this produce

                   

                  seems like touchPointID will do the trick but removeChild is what bothers me now...

                  • 6. Re: TOUCH_BEGIN calls function twice
                    Anton Azarov Community Member

                    but removeChild is what bothers me now...

                     

                    Don't worry. Simply don't use TOUCH_END with exact object. Like a movieclips.

                     

                    Use TOUCH_BEGIN for movieclip/sprite, but always use stage for TOUCH_END. This will help you controlling TOUCH_END if you removeChild before than TOUCH_END will occur.

                     

                     

                     

                    import flash.events.TouchEvent;

                    import flash.ui.Multitouch;

                    import flash.ui.MultitouchInputMode;

                    import flash.geom.Point;

                     

                    // Enable touch input mode because in devices always used Gestures as defaults

                    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

                    // Create object that will contain all user touches ids with properties

                    var touchPoints : Object = {};

                     

                     

                    // add touch listener

                    Any_DisplayObject.addEventListener(TouchEvent.TOUCH_BEGIN, TOUCH_BEGIN_FUNC);

                    stage.addEventListener(TouchEvent.TOUCH_END, TOUCH_END_FUNC);

                     

                    function TOUCH_BEGIN_FUNC (e:TouchEvent):void{

                       // save current touch point id to object

                        // exactly we save here current touch position as point

                         // note if you will use e.localX and Y - this will return local "mouseX" and "mouseY" in your MovieClip. Not global. For example your stage.stageWidth & stage.stageHeight = 960x640 and you will have there square MC in x=300 & y=100

                         // localX and Y will return not 300 + offset and Y the same. It will return from 0 to maximum X and Y depending on your square.

                     

                         // If you want know exact touch coordinates on screen - you can simple use e.stageX and e.stageY

                        touchPoints[ e.touchPointID ] = new Point(e.localX, e.localY);

                       

                        // you can use many objects so this will help determinate exact one

                        // that was touched

                        if ( touchPoints[ e.touchPointID ] ) {

                            trace ( "You touch some point in X:", touchPoints[ e.touchPointID ].x, "Y:", touchPoints[ e.touchPointID ].y );

                        }

                       

                        

                       

                    }

                     

                    function TOUCH_END_FUNC (e:TouchEvent):void{

                        // during TOUCH_END event we can check if this TOUCH_END was registered

                        // in touchPoints object

                       

                        if ( touchPoints[ e.touchPointID ] ) {

                            // if touch was registered - do your code things

                           

                            trace ( "You just released touch point saved in", touchPoints[ e.touchPointID ].x, "Y:", touchPoints[ e.touchPointID ].y );

                           

                            // you need to delete it because after touch for exact point is ends

                            // we are good programmers, we don't like unused but not free memory

                            // so simply delete information about current touchpoint in object

                            delete touchPoints[ e.touchPointID ];

                        }

                       

                        

                       

                    }

                    • 7. Re: TOUCH_BEGIN calls function twice
                      igregurec Community Member

                      thank you for all those details!

                       

                      Today I've learned a new logic therm: singleton and it seams that is just what i need here.

                       

                      I'll try with: delete touchPoints[ e.touchPointID ]; - it might help!

                      • 8. Re: TOUCH_BEGIN calls function twice
                        boat5 Community Member

                        someone might want to report it as a bug, I am using the ipod4, I just copy/pasted your code into a new document and i always returns back to 0 as it should.

                        I am running a continuous trace on i outside of the touch functions.

                         

                        I tried tapping all over, quick, slow, whole hand, simultaneous.

                         

                        No double event firing, every touch begin and touch end is heard exactly correct and once.

                        • 9. Re: TOUCH_BEGIN calls function twice
                          igregurec Community Member

                          it has a life of it's own!