-1

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?

TasostheGreat
  • 432
  • 1
  • 9
  • 24

3 Answers3

7

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
}
Community
  • 1
  • 1
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
4

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
    }
}
shanethehat
  • 15,460
  • 11
  • 57
  • 87
1

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.

laurent
  • 88,262
  • 77
  • 290
  • 428
  • There are a lot of subtle issues with this code. It works reasonably well generally, but for certain cases it will fail, causing the drag to be stuck as on even after the mouse has been released. – zzzzBov Aug 19 '11 at 16:47