Search code examples
actionscript-3

AS3 custom mouse cursor -- regular cursor not hiding


I am trying to make the standard mouse cursor disappear when y > 85

    var myCursor: Sprite;

    stage.align = StageAlign.TOP_LEFT;
   stage.scaleMode = StageScaleMode.NO_SCALE;



    function init() {
    if (y>85) {
Mouse.hide();                               // This would hide the standard cursor
    }

myCursor = new MyCursorClass();
myCursor.mouseEnabled = false;
myCursor.visible = false;

addChild(myCursor);

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
    }

    function mouseMoveHandler(evt: MouseEvent): void {
myCursor.visible = true;
myCursor.x = evt.stageX;
myCursor.y = evt.stageY;
    }

    function mouseLeaveHandler(evt:MouseEvent): void {
myCursor.visible = false;

    }
    init();

but it doesn't work.. and I don't know the reason why. Any help? I have an if statement that declares the standard mouse to be hidden when at a certain level.


Solution

  • I notice you are trying to hide the cursor when the MOUSE_LEAVE event is triggered, but you need to call the init function for every MOUSE_MOVE in order to check the if the Mouse.y is minor of y.

    This could be useful for you:

    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
    stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
    
    var myCursor:Sprite;
    myCursor = new MyCursorClass();
    myCursor.mouseEnabled = false;
    myCursor.visible = false;
    addChild(myCursor);
    
    function showCustomCursor(action:Boolean) {
      if (action) {
        Mouse.hide();
        myCursor.visible = true;
      } else {
        Mouse.show();
        myCursor.visible = false;
      }  
    }
    
    function mouseMoveHandler(evt:MouseEvent):void {
      myCursor.x = evt.stageX;
      myCursor.y = evt.stageY;
    
      // check for every mouse move event
      showCustomCursor(evt.stageY > 85); // if evt.stageY > 85
    
      evt.updateAfterEvent(); // Smoothing
    }
    
    function mouseLeaveHandler(evt:MouseEvent): void {
      showCustomCursor(false); // Show the default cursor
    }
    
    // Init
    showCustomCursor(myCursor.y > 85);