Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Detecting MOUSE_LEAVE when mouse down

Advocate ,
Oct 10, 2010 Oct 10, 2010

I need to be able to detect a MOUSE_LEAVE on the instant the mouse leaves the stage no matter if the mouse is DOWN or UP. This seems to be impossible or a limitation in AS3. The MOUSE_LEAVE only gets dispatched if mouse is UP or after the mouse is released outside stage. Is there any API like isMouseOutsideStage instead of using listeners which i can use instead? stage.addEventListener(MouseEvent.MOUSE_OUT) is also not applicable because whenever my slide show changes images, MOUSE_OUT of stage gets dispatched which is undesirable. The code below works perfectly where it detects a release or release outside if event.target is other than myButton. The weird thing or perhaps an undesirable behavior is that if i release outside stage, event.target equals myButton thus detecting a "release" instead of "release outside". I think that when we release outside stage, the want event.target to be null. There is no other work around i can think of or find.

myButton.addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void{
                MouseHandler.addEventListener(MouseEvent.MOUSE_UP, function(event:MouseEvent):void{
                    MouseHandler.removeEventListener(MouseEvent.MOUSE_UP, arguments.callee);
                    if(event.target == myButton){
                        dispatchEvent(new ButtonEvent(ButtonEvent.RELEASE));
                    }else{
                        dispatchEvent(new ButtonEvent(ButtonEvent.RELEASE_OUTSIDE));
                    }
                });
            });

I created a MouseHandler class that keeps boolean values such as isDown and tried isOnStage by setting it to true on MOUSE_MOVE and false on MOUSE_LEAVE and added the checking:

if(event.target == myButton && MouseHandler.isOnStage()){
        dispatchEvent(new ButtonEvent(ButtonEvent.RELEASE));
}else{
        dispatchEvent(new ButtonEvent(ButtonEvent.RELEASE_OUTSIDE));
}

but it can't work because MOUSE_UP is dispatched first and uses isOnStage=true before isOnStage can be set to false on MOUSE_LEAVE event.

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 10, 2010 Oct 10, 2010

you can use:

stage.addEventListener(Event.MOUSE_LEAVE,f);

function f(e:Event):void{
    trace(e);
}

stage.addEventListener(MouseEvent.MOUSE_DOWN,ff);
stage.addEventListener(MouseEvent.MOUSE_UP,fff);

function fff(e:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,ffff);
}

function ff(e:MouseEvent):void{
    stage.addEventListener(MouseEvent.MOUSE_MOVE,ffff);
}

function ffff(e:MouseEvent):void{
    if(e.stageX<0 || e.stageX>stage.stageWidth || e.stageY<0 || e.stageY>stage.stageHeight){
     

...
Translate
Community Expert ,
Oct 10, 2010 Oct 10, 2010

you can use:

stage.addEventListener(Event.MOUSE_LEAVE,f);

function f(e:Event):void{
    trace(e);
}

stage.addEventListener(MouseEvent.MOUSE_DOWN,ff);
stage.addEventListener(MouseEvent.MOUSE_UP,fff);

function fff(e:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,ffff);
}

function ff(e:MouseEvent):void{
    stage.addEventListener(MouseEvent.MOUSE_MOVE,ffff);
}

function ffff(e:MouseEvent):void{
    if(e.stageX<0 || e.stageX>stage.stageWidth || e.stageY<0 || e.stageY>stage.stageHeight){
        stage.dispatchEvent(new Event(Event.MOUSE_LEAVE));
        stage.removeEventListener(MouseEvent.MOUSE_MOVE,ffff);
    }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 10, 2010 Oct 10, 2010

cooL i didn't know that the MouseEvent class had a stageX and stageY. Thanks alot kglad! I did something similar but used MOUSE_MOVE and tried detecting Sprite.mouseX which ofcourse never gave me a value greater than stageWidth or lesser than zero. stageX/Y on the other hand helped! Thank you again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2010 Oct 10, 2010
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines