A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Custom mouse pointer rotation is erratic

  1. #1
    Member
    Join Date
    Mar 2008
    Posts
    82

    Custom mouse pointer rotation is erratic

    Ok well I'm not really replacing the mouse cursor exactly. Just placing a custom image of a pointer below it so it will follow the mouse cursor as it moves around the screen. I want this pointer image to move with the mouse and to rotate and point in the direction the mouse is traveling. Simple stuff.

    The problem is that the rotation of the pointer graphic is not smooth at all. It is jumpy and somewhat erratic particularly at slower mouse speeds. When the mouse is moving quickly the problem seems to go away.

    What is going on here?

    PHP Code:
    package {
        
    import flash.display.Sprite;
        
    import flash.events.*;
        
        public class 
    MouseFollower extends Sprite {
            private var 
    pointer:Pointer = new Pointer(); // ...a simple triangle mc
            
            
    public function MouseFollower() {
                
    addChild(pointer);
                
    stage.addEventListener(MouseEvent.MOUSE_MOVEenterFrameHandler);
            }
            
            private function 
    enterFrameHandler(moved:MouseEvent):void {    
                var 
    dx:Number mouseX pointer.x;
                var 
    dy:Number mouseY pointer.y;
                var 
    angle:Number Math.atan2(dydx) * (180/Math.PI);

                
    pointer.rotation angle;
                
    pointer.mouseX;
                
    pointer.mouseY;
            }
        }

    Last edited by Trugas; 11-09-2009 at 07:31 PM. Reason: Simplifying code

    ----------------------------------------------------------------------1
    5-------------3-------------2-------------1-------------0-------------0
    -------0-------------0-------------0-------------0-------------0------0
    ----------------------------------------------------------------------0
    3-------------2-------------0-------------0---------------------------2
    --------------------------------------------------------3-------------3

  2. #2
    Member
    Join Date
    Mar 2008
    Posts
    82
    Ok, so to explain exactly what I'm seeing: The movieclip graphic following under the mouse is not rotating smoothly. What actually appears to be happening is that the graphic is snapping to 45 degree increments at slow speeds. This only gives eight steps in a rotation. Again, this only happens when the mouse is moving slowly.

    When the mouse is moving in a direction that is not exactly horizontal, vertical or diagonal it flickers between the nearest two of these directions.

    ----------------------------------------------------------------------1
    5-------------3-------------2-------------1-------------0-------------0
    -------0-------------0-------------0-------------0-------------0------0
    ----------------------------------------------------------------------0
    3-------------2-------------0-------------0---------------------------2
    --------------------------------------------------------3-------------3

  3. #3
    Member
    Join Date
    Mar 2008
    Posts
    82
    Ok I think I found the problem. Mouse coords are only integers not numbers. This lack of precision means that the smallest distance the mouse can move on the x or y axis is exactly 1 px (or -1px), of course it can also not move at all (0 px). These values can only give eight solutions (left, right, up, down and the four diagonals). So the graphic will only point in one of these eight directions when the mouse is moving very slowly.

    Any ideas about how I can get around this?

    ----------------------------------------------------------------------1
    5-------------3-------------2-------------1-------------0-------------0
    -------0-------------0-------------0-------------0-------------0------0
    ----------------------------------------------------------------------0
    3-------------2-------------0-------------0---------------------------2
    --------------------------------------------------------3-------------3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center