Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何使用actionscript 3使对象相对于鼠标指针移动?_Actionscript 3 - Fatal编程技术网

Actionscript 3 如何使用actionscript 3使对象相对于鼠标指针移动?

Actionscript 3 如何使用actionscript 3使对象相对于鼠标指针移动?,actionscript-3,Actionscript 3,我正在做一个游戏,我想让我的角色在向前按鼠标指针时移动,并用相应的箭头键左右扫射 这是我目前的代码: import flash.events.MouseEvent; //Event Listners stage.addChild(crosshair_mc); crosshair_mc.mouseEnabled = false; crosshair_mc.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor); function fl_

我正在做一个游戏,我想让我的角色在向前按鼠标指针时移动,并用相应的箭头键左右扫射

这是我目前的代码:

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;
    }

}

旋转部分是正确的,现在只需将其与
cos
sin
分别合并到
x
y
轴上。例如:

var speed:Number = 8;
var angle:Number = Math.atan2(mouseY - character_mc.y, mouseX - character_mc.x);

character_mc.rotation = angle * 180 / Math.PI;
character_mc.x += Math.cos( angle ) * speed;
character_mc.y += Math.sin( angle ) * speed;
为了避免混淆,我将停止在旋转中添加
90
度,而是将图形旋转到右/东方向

扫射使用相同的逻辑,你只需要在你想要扫射的任何方向的旋转上加四分之一个圆。仅供参考,以弧度表示的圆的四分之一是π/2。一个圆中有2个圆周率弧度:

// Augment angle for strafing.
angle += Math.PI / 2;

@user2130844但是你想用你的代码来实现它…是的,但是我如何实现它使它达到我想要的效果呢?因为我之前用它替换了旧代码,它使字符_mc遵循cursor@user2130844是的,所以当你像以前一样按下相应的键时,只需对x和y进行更新原始代码。你能告诉我如何将代码融入到我的代码中以达到预期的效果吗?