4 Replies Latest reply on Oct 14, 2009 11:33 AM by flairjax

    Sprite to following mouse has a delay.

    flairjax Level 1

      I am using an enterFrame to get a Sprite to follow the mouse but there is a delay ( i also tried mousemove and updateAfter.. but it still has a delay).

       

      My main stage scales (zooms) from .2 to 1 and when the scale is at 1 the lag or delay isn't to bad (5 to 10) pixels, but when zoomed down to .20 the lag/delay is 50 to 100 pixels.

       

      Is there a better way to get the sprite to follow the mouse?  I tried startDrag but I can't use that because it pulss the sprite away from its attached line (think a candy sucker) the top just comes of the stick with startDrag.

       

      TIA, J

        • 1. Re: Sprite to following mouse has a delay.
          Chet Haase Level 3

          The default frame rate (at which enterFrame will be called) is usually good enough for most rendering, but not good enough for fast mouse interaction. You'd probably do better by handling the MouseEvent.MOVE event and then calling updateAfterEvent(), which will force an update with the new sprite position sooner than it would happen via the normal frame rate mechanism.

           

          Chet.

          1 person found this helpful
          • 2. Re: Sprite to following mouse has a delay.
            flairjax Level 1

            Tried that but same result, here is the code.

             

                // Add Listeners
                        tempNode.stage.addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMoveEvent, false, 5000);
                        tempNode.addEventListener( MouseEvent.MOUSE_UP, stopFollowingAndReAttach );

             

            private function handleMouseMoveEvent( e:MouseEvent ) : void
                    {
                        var pt:Point = tempNode.localToGlobal( new Point ( 0, 0 ) );
                        var vx:Number = (e.stageX) - pt.x;
                        var vy:Number = (e.stageY) - pt.y;
                        tempNode.x += vx;
                        tempNode.y += vy;
                        e.updateAfterEvent();
                    }
                   
                    private function stopFollowingAndReAttach( e:Event ) : void
                    {
                        tempNode.removeEventListener( MouseEvent.MOUSE_UP, stopFollowingAndReAttach );
                        tempNode.stage.removeEventListener( MouseEvent.MOUSE_MOVE, handleMouseMoveEvent );
                    }

            • 3. Re: Sprite to following mouse has a delay.
              Chet Haase Level 3

              I'm not sure what you're seeing since I haven't seen the code. But one of the problems in the code below (at least when I tried to get it working) is that you're snapping the tmpoNode registration point (its (0,0) point) to wherever the mouse clicked. What you want to do instead is register the original click point, then track the delta movement each time, adjusting the tmpNode position by that delta. Here's the code that worked for me:

               

                      private var prevX:Number;

                      private var prevY:Number;

                      private function mouseDownHandler(e:MouseEvent):void

                      {

                          addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMoveEvent, false, 5000);

                          addEventListener( MouseEvent.MOUSE_UP, stopFollowingAndReAttach );

                          prevX = e.stageX;

                          prevY = e.stageY;

                      }

                           

                      private function handleMouseMoveEvent( e:MouseEvent ) : void

                      {

                          var pt:Point = rect.localToGlobal( new Point ( 0, 0 ) );

                          var vx:Number = (e.stageX) - prevX;

                          var vy:Number = (e.stageY) - prevY;

                          rect.x += vx;

                          rect.y += vy;

                          prevX = e.stageX;

                          prevY = e.stageY;

                          e.updateAfterEvent();

                      }

               

              Given that fix, I didn't see any tracking problems (performance or otherwise).

               

              The other problem I saw in my code (which may differ widely from yours, of course) is that the code doesn't adjust for the scale factor on the stage. You have to apply the same scaling factor to the amount moved to make sure you're adjusting to the stage's coordinates. To avoid any error accumulation, this might be more effective if you track the movement from the original click point rather than creating a delta between each MOVE handling.

               

              Chet.

              1 person found this helpful
              • 4. Re: Sprite to following mouse has a delay.
                flairjax Level 1

                Ok got it.

                 

                     .............

                    // Add Listeners
                            tempNode.stage.addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMoveEvent, false, 5000);
                            tempNode.addEventListener( MouseEvent.MOUSE_DOWN, handleMouseDownEvent, false, 50001 );
                        }
                       
                       
                        private function handleMouseMoveEvent( e:MouseEvent ) : void
                        {
                            var pt:Point = tempNode.localToGlobal( new Point ( 0, 0 ) );
                            var vx:Number = ( (e.stageX) - pt.x ) / _gm.scaleX;
                            var vy:Number = ( (e.stageY) - pt.y ) / _gm.scaleY;
                            tempNode.x += vx;
                            tempNode.y += vy;
                            e.updateAfterEvent();
                        }