0

我正在制作一个游戏,我希望我的角色在向前按下时向鼠标指针移动,并用相应的箭头键向左和向右扫射。

这是我当前的代码:

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

}
4

1 回答 1

1

你有正确的旋转部分,现在你需要的只是将它分别与和轴cos结合起来。例子:sinxy

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在旋转上添加度数,而是将图形旋转到向右/向东。

扫射的东西使用相同的逻辑,你只想在你想要扫射的任何方向上添加四分之一圆。仅供参考,四分之一圆的弧度是 PI / 2。一个圆中有 2 个 PI 弧度:

// Augment angle for strafing.
angle += Math.PI / 2;
于 2013-03-04T07:44:57.020 回答