0

我正在创建一个使用三角函数来计算和显示动态文本框中的距离和度数的游戏。我正在计算光标与影片剪辑中心的距离。并且使用影片剪辑的中心,当我的光标在 swf 周围移动时,我试图计算并显示一个完整的 360º。我有游戏的距离部分工作,但显示度数的部分工作不正常。动态文本框仅从 90º 到 270º 显示。它不是从 270º 到 360º/0º 到 90º,而是从 270º 倒数到 90º。下面是我的动作脚本。我非常感谢任何帮助或建议。谢谢!

//Mouse and Dynamic Text Boxes-------------------------

Mouse.hide();

onMouseMove = function () {
feedback.text = "You are moving your mouse";
cursor._x = _xmouse;
cursor._y = _ymouse;
updateAfterEvent();
xmouse_value.text = Math.atan2((a), (b));
ymouse_value.text = Math.round(radians*180/Math.PI)
updateAfterEvent();  
};

Mouse.addListener(myListener);


//distance (RANGE)
_root.onEnterFrame = function () {
xmid = Stage.width/2;
ymid = Stage.height/2;

a = _root._ymouse-ymid;
b = _root._xmouse-xmid;
c = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
feedbacka.text = Math.round(a);
feedbackb.text = Math.round(b);
feedbackc.text = Math.round(c/30.4);

updateAfterEvent();  

var radians:Number;
var degrees:Number;

//Calculcate Radians
//Radians specify an angle by measuring the length around the path of the circle.
radians = Math.atan2((c), (b))

//calculate degrees
//the angle the circle is in relation to the center point
//update text box inside circle
radians_txt = Math.round(radians*360/Math.PI);
degrees_txt = Math.round(radians*180/Math.PI);

updateAfterEvent();  

//getting past 270 degrees

radians2_txt = Math.round(radians/Math.PI);
radians2_txt = Math.floor(radians + -270);

}
4

1 回答 1

0

to 的参数atan2应该是两点之间的 delta-y 和 delta-x,但是您传递的是两点之间的距离和 delta-x。试试这个:

radians = Math.atan2(a, b);

下一个问题是将弧度转换为度数。要将弧度转换为度数,您可以这样做:

degrees_txt = radians * 180 / Math.PI;

请注意,atan2从之间返回-Math.PI / 2Math.PI / 2。转换为度数时,此范围变为 -180 到 180。要转换为 0 到 360,如果结果为负,您可以将 360 添加到结果中:

if(degrees_txt < 0) degrees_txt += 360;
于 2012-04-05T02:27:50.533 回答