Packageflash.ui
Classpublic final class Mouse
InheritanceMouse Inheritance Object

The methods of the Mouse class are used to hide and show the mouse pointer (cursor). The Mouse class is a top-level class whose properties and methods you can access without using a constructor. The pointer is visible by default, but you can hide it and implement a custom pointer.

View the examples.

See also
flash.events.MouseEvent


Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods
 FunctionDefined by
 Inherited
Indicates whether an object has a specified property defined.
Object
  
[static] Hides the pointer.
Mouse
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
[static] Displays the pointer.
Mouse
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Method detail
hide method

public static function hide():void

Hides the pointer. The pointer is visible by default.

See also
flash.display.DisplayObject.mouseX, flash.display.DisplayObject.mouseY
show method

public static function show():void

Displays the pointer. The pointer is visible by default.

See also
flash.display.DisplayObject.mouseX, flash.display.DisplayObject.mouseY
Class examples

The following example uses the classes MouseExample, SimpleButton, ButtonDisplayState and CustomCursor to place a simple button on the stage that has a custom cursor and changes when clicked. This is accomplished using the following steps:
  1. Declare instance properties cursor of type CustomCursor, child of type CustomButton, and gutter of type uint.
  2. Assign child to a new CustomButton instance and set its x and y coordinates to 10 pixels each and then adds the instance to the display list. CustomButton overrides the downState, upState, overState, and hitTestState properties in SimpleButton. Each of these properties instantiates a ButtonDisplayState object, which draws a different square, depending on the child's state
  3. child is then used to add a mouseOver event listener and listener method mouseOverHandler(), along with a mouseOut event listener and associated method mouseOutHandler().
  4. The event listeners work as follows:
    1. mouseOverHandler: hides the "normal" mouse pointer and adds a mouseMove listener, which processes the mouse moves using mouseMoveHandler(), which is described below.
    2. mouseOutHandler: using this code, when the mouse moves outside the custom button, the "normal" mouse pointer is shown, the mouseMove event listener is removed, and the custom cursor's visibility is set to false.
    3. mouseMoveHandler: moves the custom cursor around wherever the mouse pointer is moved and sets the custom cursor's visibility to true.
  5. Back in the MouseExample constructor, the cursor property is assigned to a new CustomCursor object and then added to the display list using addChild(). CustomCursor draws a small nearly black square in place of the "normal" mouse pointer whenever the mouse is over child.
  6. A fourth event listener of type mouseLeave is added, with the associated method mouseLeaveHandler(). In this method, (called if the mouse leaves the stage), mouseOutHandler() is passed a new mouseMove listener object, which essentially removes the mouse pointer so it is not "left" on the stage.
package {
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.ui.Mouse;
    import flash.events.*;

    public class MouseExample extends Sprite {
         private var cursor:CustomCursor;
         private var child:CustomButton;
         private var gutter:uint = 10;

        public function MouseExample() {
            child = new CustomButton();
            child.x = gutter;
            child.y = gutter;
            addChild(child);

            child.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
            child.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

            cursor = new CustomCursor();
            addChild(cursor);

            stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
        }

        private function mouseOverHandler(event:MouseEvent):void {
            trace("mouseOverHandler");
            Mouse.hide();
            child.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        }

        private function mouseOutHandler(event:MouseEvent):void {
            trace("mouseOutHandler");
            Mouse.show();
            child.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            cursor.visible = false;
        }

        private function mouseMoveHandler(event:MouseEvent):void {
            trace("mouseMoveHandler");
            cursor.x = event.localX;
            cursor.y = event.localY;
            event.updateAfterEvent();
            cursor.visible = true;
        }

        private function mouseLeaveHandler(event:Event):void {
            trace("mouseLeaveHandler");
            mouseOutHandler(new MouseEvent(MouseEvent.MOUSE_MOVE));
        }
    }
}

import flash.display.Shape;
import flash.display.SimpleButton;
    
class CustomButton extends SimpleButton {
    var upColor:uint = 0xFFCC00;
    var overColor:uint = 0xCCFF00;
    var downColor:uint = 0x00CCFF;
    var size:uint = 80;
    
    public function CustomButton() {
        downState = new ButtonDisplayState(downColor, size+10);
        overState = new ButtonDisplayState(overColor, size);
        upState = new ButtonDisplayState(upColor, size);
        hitTestState = new ButtonDisplayState(upColor, size);
    }
}

class ButtonDisplayState extends Shape {
    var bgColor:uint;
    var size:uint;
    
    public function ButtonDisplayState(bgColor:uint, size:uint) {
        this.bgColor = bgColor;
        this.size = size;
        draw();
    }
    
    private function draw():void {
        graphics.clear();
        graphics.beginFill(bgColor);
        graphics.drawRect(0, 0, size, size);
        graphics.endFill();
    }
}
    
class CustomCursor extends Shape {
    var bgColor:uint = 0x333333;
    var size:uint = 10;
    
    public function CustomCursor() {
        visible = false;
        draw();
    }
    
    private function draw():void {
        graphics.clear();
        graphics.beginFill(bgColor);
        graphics.drawRect(0, 0, size, size);
        graphics.endFill();
    }
}




 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/labs/flashauthoringpreview/flash/ui/Mouse.html