Actionscript 3将对象旋转到鼠标

时间:2016-04-30 13:12:05

标签: flash actionscript adobe

您好我在youtube上看了tut并且我编写了这段代码以使对象旋转到鼠标位置,但它有点无法正常工作。

代码:

package  {

import flash.display.MovieClip;
import flash.events.Event;

public class char extends MovieClip
{
    private var _pointer = this;

    public function char()
    {
        addEventListener(Event.ENTER_FRAME, onEnter);
    }

    private function onEnter(event:Event):void
    {
        //Initialize dx and dy variables
        var dx:Number = mouseX - _pointer.x;
        var dy:Number = mouseY - _pointer.y;

        //Store angle in angle variable
        var angle:Number = Math.atan2(dy, dx) * 180 / Math.PI;

        //Apply angle
        _pointer.rotation = angle;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您需要从父级影片剪辑中获取mouseX和mouseY坐标,因为:

  1. _pointer.x和_pointer.y也在父级坐标系中表示
  2. 动画的旋转会干扰mouseX和mouseY值(它们会反映未旋转的对象)
  3. 所以你需要修改这两行:

    var dx:Number = parent.mouseX-_pointer.x;
    var dy:Number = parent.mouseY-_pointer.y;