A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: How do i make a cursor that rotates with mouse movements?

Hybrid View

  1. #1
    Junior Member
    Join Date
    Nov 2007
    Posts
    24

    How do i make a cursor that rotates with mouse movements?

    Okay, this is what i've managed to do so far:

    http://jpoel.byethost15.com/Swim/Swim_example.swf

    But as you can see it isnt smooth at all! Does anyone have any better code i can use?

    This is the code i have written to do it:

    Code:
    var px = mouseX;
    var py = mouseY;
    this.addEventListener(MouseEvent.MOUSE_MOVE,moveMouse);
    function moveMouse(e:MouseEvent):void{
    	var a = mouseY - py;
    	var b = mouseX - px;
    	var radians = Math.atan2(a,b);
    	var degrees = radians / (Math.PI / 180);
    	Cursor.rotation = degrees;
    	Mouse.hide();
    	Cursor.x = mouseX;
    	Cursor.y = mouseY;
    	px = mouseX;
    	py = mouseY;
    }


    FLA: http://jpoel.byethost15.com/Swim/Swim_example.fla (you might have to right click on that and click "Save target as"

  2. #2
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    That code looks like a good start. The problem is that I can't access your .swf, it just goes to some server notification... this means I can't see what issues you might be having.

    The only thing that might need changing is to have

    Cursor.rotation += degrees

    as opposed to just "= degrees".

    If you feel ok about posting your .fla file I'd be happy to have a closer look!
    Check out my blog showing the development of my flash game, the Dregs of War

  3. #3
    Junior Member
    Join Date
    Nov 2007
    Posts
    24
    Sorry! try this link:
    http://www.swfcabin.com/open/1257616898

    as you can see it works, but the mouse's movement is really juttery ;( just need to find a way to make it smoother

    ALSO: changing = to += made the mouse movements even worse! haha
    Last edited by JamesPoel; 11-07-2009 at 02:48 PM.

  4. #4
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    it only seems to jitter if I'm going very slowly, because the differences between each frame are greater...

    How about instead of just setting the rotation to the dregrees value, you can set a "targetAngle" variable to that, then afterwards you have the cursor rotate towards that angle at a speed you've defined (say 20 degrees per frame). If the difference between the cursor's current angle and the target angle is less than that speed, you just set it's rotation to the target angle. Did that make sense?
    Check out my blog showing the development of my flash game, the Dregs of War

  5. #5
    Junior Member
    Join Date
    Nov 2007
    Posts
    24
    Quote Originally Posted by Awoogamuffin View Post
    it only seems to jitter if I'm going very slowly, because the differences between each frame are greater...

    How about instead of just setting the rotation to the dregrees value, you can set a "targetAngle" variable to that, then afterwards you have the cursor rotate towards that angle at a speed you've defined (say 20 degrees per frame). If the difference between the cursor's current angle and the target angle is less than that speed, you just set it's rotation to the target angle. Did that make sense?
    haha that didnt make much sense at all im afraid :$ im a bit noobish with AS3 when it gets to a certain depth, could you point in the right direction with some code or something?

  6. #6
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    In a tutorial I wrote a while ago, I had an AIPlayer that would rotate a ship to face another ship.

    There's a function that figures out what the angle is between the AIPlayer and the its target, then another function (called faceAngle) which will the make the player start turning in that direction.

    So go here:

    http://www.flashgametutorials.blogspot.com/

    scroll down the AIPlayer class, and find the function faceAngle(), and that could point you in the right direction...
    Check out my blog showing the development of my flash game, the Dregs of War

  7. #7
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Another idea that could be easier is to have an array which stores the values of the degree variable for the last 5 frames, and have the cursor rotation be the average of those values. that might make for a smoother experience...

    [EDIT] Oh and you could try raising the frames per second
    Check out my blog showing the development of my flash game, the Dregs of War

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