ActionScript 3 鼠标按下

发布于 2024-11-30 11:39:56 字数 126 浏览 2 评论 0原文

我如何不断检查鼠标是否按下,如果鼠标按下并且移动,我

尝试了 mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,fct) 函数,但它只调用函数,然后停下来,我想不断地做,我该怎么做?

How could I check constantly whether mouse is down and if mouse is down and it moves that I cal a function

I tried mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,fct) but it only calls the function ones and then stops, I want to do it constantly, how can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

养猫人 2024-12-07 11:39:56

您所要求的内容以前已经被要求过,但我很难找到重复的问题,因此我将发布我所在问题的链接就一些利基问题寻求帮助

在 AS3 中处理拖动事件的一些合理代码是:

stage.addEventListener( MouseEvent.MOUSE_DOWN, beginDrag );

function beginDrag( e:MouseEvent )
{
  stage.addEventListener( MouseEvent.MOUSE_MOVE, drag );
  stage.addEventListener( MouseEvent.MOUSE_UP, endDrag );
  stage.addEventListener( MouseEvent.DEACTIVATE, endDrag );
  stage.addEventListener( Event.MOUSE_LEAVE, endDrag );
  stage.addEventListener( Event.REMOVED_FROM_STAGE, stageEndDrag );

  //trigger beginDrag event
}
function drag( e:MouseEvent )
{
  //trigger drag event
}
function endDrag( e:Event )
{
  stage.removeEventListener( MouseEvent.MOUSE_MOVE, drag );
  stage.removeEventListener( MouseEvent.MOUSE_UP, endDrag );
  stage.removeEventListener( MouseEvent.DEACTIVATE, endDrag );
  stage.removeEventListener( Event.MOUSE_LEAVE, endDrag );
  stage.removeEventListener( Event.REMOVED_FROM_STAGE, stageEndDrag );

  //trigger endDrag event
}

What you're asking for has been asked for before, but I can't easily find a duplicate question, so I'll post the link to my question where I was asking for help with a few niche issues.

Some reasonable code for handling drag events in AS3 is:

stage.addEventListener( MouseEvent.MOUSE_DOWN, beginDrag );

function beginDrag( e:MouseEvent )
{
  stage.addEventListener( MouseEvent.MOUSE_MOVE, drag );
  stage.addEventListener( MouseEvent.MOUSE_UP, endDrag );
  stage.addEventListener( MouseEvent.DEACTIVATE, endDrag );
  stage.addEventListener( Event.MOUSE_LEAVE, endDrag );
  stage.addEventListener( Event.REMOVED_FROM_STAGE, stageEndDrag );

  //trigger beginDrag event
}
function drag( e:MouseEvent )
{
  //trigger drag event
}
function endDrag( e:Event )
{
  stage.removeEventListener( MouseEvent.MOUSE_MOVE, drag );
  stage.removeEventListener( MouseEvent.MOUSE_UP, endDrag );
  stage.removeEventListener( MouseEvent.DEACTIVATE, endDrag );
  stage.removeEventListener( Event.MOUSE_LEAVE, endDrag );
  stage.removeEventListener( Event.REMOVED_FROM_STAGE, stageEndDrag );

  //trigger endDrag event
}
似梦非梦 2024-12-07 11:39:56

那么你想要做的是当鼠标移动并且按钮按下时调用一个函数?

最简单的方法是在鼠标移动时调用该函数(或者如果您想在鼠标未移动时调用该函数,则使用计时器),并让它在执行任何操作之前检查由鼠标向上/向下设置的标志行动。

var isDown:Boolean = false;

stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);

function onMouseDown(evt:MouseEvent):void
{
    isDown = true;
}

function onMouseUp(evt:MouseEvent):void
{
    isDown = false;
}

function onMouseMove(evt:MouseEvent):void
{
    if(isDown) {
        //party
    }
}

So what you want to do is call a function when the mouse moves and the button is down?

The easiest way is to call the function on mouse move (or use a Timer if you want to call the function even when the mouse isn't moving), and have it check a flag set by the mouse up/down before it takes any action.

var isDown:Boolean = false;

stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);

function onMouseDown(evt:MouseEvent):void
{
    isDown = true;
}

function onMouseUp(evt:MouseEvent):void
{
    isDown = false;
}

function onMouseMove(evt:MouseEvent):void
{
    if(isDown) {
        //party
    }
}
葬心 2024-12-07 11:39:56

我认为您可以简单地在鼠标按下时设置一些标志,并在鼠标抬起时再次将其设置回来。像这样:

private var down_:Boolean = false;

mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);

function onMouseDown(event) {
    down_ = true;   
    mk_mc.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}

function onMouseUp(event) {
    down_ = false;  
    mk_mc.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}

然后只需轮询 down_ 即可知道鼠标是否按下。

I think you could simply set some flag when the mouse is down and set it back again when the mouse is up. Something like that:

private var down_:Boolean = false;

mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);

function onMouseDown(event) {
    down_ = true;   
    mk_mc.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}

function onMouseUp(event) {
    down_ = false;  
    mk_mc.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}

Then just poll down_ to know if the mouse is down or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文