好吧所以我有一个名为character_mc的角色,当你按下向前箭头并相对于它的直角进行扫描时,我希望它朝着鼠标移动。
我对动作脚本很新,所以请你在原始代码中加入代码示例
这是我目前的代码:
import flash.events.MouseEvent;
//Event Listners
stage.addChild(crosshair_mc);
crosshair_mc.mouseEnabled = false;
crosshair_mc.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
crosshair_mc.x = stage.mouseX;
crosshair_mc.y = stage.mouseY;
}
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE,facecursor);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
//Functions
function facecursor(event):void
{
character_mc.rotation = (180 * Math.atan2(mouseY - character_mc.y,mouseX - character_mc.x))/Math.PI + 90;
}
function fl_KeyboardDownHandler(event:KeyboardEvent):void
{
trace("Key Code Pressed: " + event.keyCode);
if (event.keyCode == 38)
{
character_mc.y = character_mc.y - 5;
}
if (event.keyCode == 40)
{
character_mc.y = character_mc.y + 5;
}
if (event.keyCode == 39)
{
character_mc.x = character_mc.x + 5;
}
if (event.keyCode == 37)
{
character_mc.x = character_mc.x - 5;
}
}
答案 0 :(得分:1)
我可以告诉你如何做到这一点的基本概念,但你必须将它应用到你自己的代码中。要涉及转换运动代码以使用矢量,然后修改矢量以获得面向鼠标的方向(或与该方向成直角)和一点点数学。
现在,您只能在每个按键情况下沿着x和y轴直线移动角色。左/右仅沿X移动,向上/向下仅沿Y移动。
要向鼠标移动,当按下向上/向下/向左/向右键时,要求角色沿X和Y移动。很明显,你可以看到你是否将角色的x / y位置移动相同的量,比如5,然后它将精确地移动45度(尽管它实际上会移动7.07像素的步长,希望你能看到原因) 。您可以将其表示为向量:(5,5)。您可以使用Point对象来表示此向量:
var movementVector:Point = new Point(5, 5);
trace(movementVector.x); // gives 5
trace(movementVector.y); // also gives 5
考虑到这一点,您还可以使用矢量来表示y轴上直线上下移动:
// set the x to 0 and y to 5
movementVector.x = 0; // 0 would mean not to move the character along the x
movementVector.y = 5; // using -5 would move the character up
仅沿x轴移动:
movementVector.x = 5; // using -5 would move the character right
movementVector.y = 0; // 0 would mean not to move the character along the y
除了使用向量的值之外,角色的实际移动与现在的移动相同:
character_mc.x = character_mc.x + movementVector.x;
character_mc.y = character_mc.y + movementVector.y;
现在要弄清楚从角色的位置到鼠标位置的对角线移动的正确向量非常简单。向量的x值是从字符到鼠标的x距离,向量的y值是从字符到鼠标的y距离。
假设字符为125,100,鼠标为225,150。这意味着字符和鼠标之间的距离为100,50 x和y。因此,你最终会得到一个向量:
movementVector.x = 100;
movementVector.y = 50;
如果您要将此矢量原样应用于角色的位置,它将立即到达鼠标(然后超越它),因为角色沿x移动100个像素,沿着x移动50个像素你马上。步长大小为111.8像素。您需要将其缩小到角色的速度。您可以通过调用Point类上的normalize()方法来缩小向量:
trace(movementVector.x); // gives 100
trace(movementVector.y); // gives 50
// assuming '5' is the max speed of the character
movementVector.normalise(5);
trace(movementVector.x); // gives 4.47213595499958
trace(movementVector.y); // gives 2.23606797749979
这将导致“步进”大小为5。应用此选项会使角色向右移动5个像素,向右移动100个像素,向下移动50个像素。
要将矢量精确地转换为90度,一种快速而简单的方法是交换x和y值。
如果你对normalize()方法在数学上做了什么感到好奇,那就是它采用向量(或点)的x和y值并将其除以长度以得到单位向量(或带有步长的向量) 1)的大小,然后是你给它的输入的时间,以便将它缩放到所需的长度。
答案 1 :(得分:0)
要将character_mc
移向鼠标点,您只需要两者之间的方向向量:
var dir:Point = new Point(mouseX - character_mc.x, mouseY - character_mc.y);
dir.Normalize();
// The following should be called when the 'up' or 'forward' arrow is pressed
// to move the character closer to mouse point
character_mc.x += dir.x; // dir can be multiplied by a 'speed' variable
character_mc.y += dir.y;
围绕该点左右扫射更棘手:
// Where radius is the distance between the character and the mouse
character_mc.x = mouseX + radius * Math.cos(rad);
character_mc.y = mouseY + radius * Math.sin(rad);
您应该会发现本教程很有用,因为它会执行您描述的所有内容以及更多内容 http://active.tutsplus.com/tutorials/actionscript/circular-motion-in-as3-make-one-moving-object-orbit-another/