如何使我的性格相对移动使用ActionScript 3鼠标的位置? [英] How to make my character move relative to the mouse position with actionscript 3?

查看:87
本文介绍了如何使我的性格相对移动使用ActionScript 3鼠标的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行,所以我有一个角色叫character_mc,我希望它朝着移动鼠标时,preSS前进箭头和扫射相对于该直角。

我很新的动作,以便可以请包括与例如在我原来的code本code

下面是我目前的code:

 进口flash.events.MouseEvent;
//事件Listners
stage.addChild(crosshair_mc);
crosshair_mc.mouseEnabled = FALSE;
crosshair_mc.addEventListener(Event.ENTER_FRAME,fl_CustomMouseCursor);

功能fl_CustomMouseCursor(事件:事件)
{
    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);
//功能
功能facecursor(事件):无效
{
    character_mc.rotation =(180 * Math.atan2(mouseY的 -  character_mc.y,mouseX  -  character_mc.x))/ Math.PI + 90;

}


功能fl_KeyboardDownHandler(事件:KeyboardEvent的):无效
{
    跟踪(键code pressed:+ event.key code);
    如果(event.key code == 38)
    {
        character_mc.y = character_mc.y  -  5;
    }
    如果(event.key code == 40)
    {
        character_mc.y = character_mc.y + 5;
    }
        如果(event.key code == 39)
    {
        character_mc.x = character_mc.x + 5;
    }
        如果(event.key code == 37)
    {
        character_mc.x = character_mc.x  -  5;
    }

}
 

解决方案

要移动你的 character_mc 对鼠标指向你只需要在两者之间的方向矢量:

  VAR DIR:点=新的点(mouseX  -  character_mc.x,mouseY的 -  character_mc.y);
dir.Normalize();

//当向上或前进箭头是pressed应该叫下面的
//移动人物更贴近鼠标点
character_mc.x + = dir.x; // DIR可以通过'速度'变量乘以
character_mc.y + = dir.y;
 

扫射左,右绕点是有点棘手:

  //其中半径是性格和鼠标之间的距离
character_mc.x = mouseX +半径* Math.cos(RAD);
character_mc.y = mouseY的+半径* Math.sin(RAD);
 

,因为它做的一切你描述,更应该发现本教程非常有用: <一href="http://active.tutsplus.com/tutorials/actionscript/circular-motion-in-as3-make-one-moving-object-orbit-another/" rel="nofollow">http://active.tutsplus.com/tutorials/actionscript/circular-motion-in-as3-make-one-moving-object-orbit-another/

ok so i have a character called character_mc and i want it to move towards the mouse when you press the forward arrow and strafe relative to right angles of that.

i am quite new to actionscript so could you please include and example of your code in my original code

Here is my current code:

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

}

解决方案

To move your character_mc towards the mouse point you only need the direction vector between the two:

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;

Strafing left and right around the point is a little more tricky:

// 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);

You should find this tutorial useful as it does everything you describe and more: http://active.tutsplus.com/tutorials/actionscript/circular-motion-in-as3-make-one-moving-object-orbit-another/

这篇关于如何使我的性格相对移动使用ActionScript 3鼠标的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆