我有一个炮塔,我想用炮塔旋转并面向鼠标来限制旋转,所以它不会越过最大旋转并在达到最大点时停止旋转。并且当达到最小度数时不会低于最小度数,但最大和最小角度会根据鼠标所面对的方向而改变。
当鼠标位于炮塔的右侧时,又名mouseX大于炮塔的x位置。 炮塔将改变scaleX,炮塔向右转,并限制瞄准旋转在120度和-160之间。
当炮塔朝左时,又称鼠标X小于炮塔的x位置。炮塔将切换scaleX并向左转。然后,受约束的瞄准旋转切换到70度到-20度之间。
这是我的代码,我是计算旋转和度数的新手,所以我认为我完全错了,因为除非我删除代码,否则炮塔根本不会旋转。我需要帮助修复代码。谢谢!
function update(e:Event)
{
//make the turret face the mouse
if (parent != null)
{
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy, dx)/Math.PI * 180;
//this is supposed to constrain the rotation if the mouse is bigger than turret's x
//position, else constrain it the other way, this part of the code doesnt work.
//this code makes it so the turret is unable rotate at all
//if i remove the code, the turret can rotate freely though.
if(mouseX > x)
{
angle=Math.min(angle,120);
angle=Math.max(angle,-160);
}
else
{
angle=Math.min(angle,-20);
angle=Math.max(angle,70);
}
rotation = angle;
}
}
答案 0 :(得分:0)
您尚未指定如何调用update
。如果它被(比方说)鼠标移动事件调用,您可以直接从事件中获取鼠标坐标。
这是另一个使用该技术的版本。脚本位于“炮塔”的时间线中。动画片段。
function update(e:MouseEvent)
{
//make the turret face the mouse
if (parent != null)
{
var dx = e.stageX - x;
var dy = e.stageY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
//this is supposed to constrain the rotation if the mouse is bigger than turret's x
//position, else constrain it the other way, this part of the code doesnt work.
//this code makes it so the turret is unable rotate at all
//if i remove the code, the turret can rotate freely though.
if (mouseX > x)
{
angle = Math.min(angle,120);
angle = Math.max(angle,-160);
}
else
{
angle = Math.min(angle,-20);
angle = Math.max(angle,70);
}
rotation = angle;
}
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, update);