PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Rotate MovieClip towards mouse
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Rotate MovieClip towards mouse

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3

    Cow Icon Rotate MovieClip towards mouse

    I made a game in AS2 where the player rotates towards the mouse, then discovered you need AS3 for 3D things(which I plan to add later) so I re-made the game in AS3. I think I can more or less do the rest of it but the rotation is driving me crazy. This code works fine in AS2 but when I try it in AS3 it completely fails. The player just wobbles about a 90 degree radius.

    Actionscript Code:
    [CODE]var playerX:Number = this.x;
                var playerY:Number = this.y;
                var rotationDirection:Number = Math.round(180 - Math.atan2(mouseX - playerX,mouseY - playerY) * 180 / Math.PI);
                this.rotation = rotationDirection;[/CODE]

    (I have changed the mouse._x to mouseX and stuff)


    If you can see any major errors, please let me know before I go completely insane.

  2. #2
    Senior Member
    Join Date
    Jan 2009
    Posts
    208
    Actionscript Code:
    var arrow:MovieClip = new Arrow ();
    arrow.x = stage.stageWidth/2;
    arrow.y = stage.stageHeight/2;
    addChild(arrow);

    this.addEventListener(Event.ENTER_FRAME, onLoop, false, 0 ,true);
    function onLoop(e:Event):void {
        arrow.rotation = getAngle(arrow.x, arrow.y, mouseX, mouseY);
    }
    function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number {
        var radians:Number = Math.atan2(y1-y2, x1-x2);
        return rad2deg(radians);
    }
    function rad2deg(rad:Number):Number {
        return rad * (180 / Math.PI);
    }

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