---
title: "简单模拟MouseEvent.ROLL_OUT MouseEvent.ROLL_OVER MouseEvent.MOUSE_MOVE"
author: "wgcode"
date: "2011-01-13T10:55:56Z"
tags: []
categories: ["编程语言"]
slug: "mouseevent-roll-out-mouseevent-roll-over-mouseevent-mouse-move"
draft: false
lastmod: "2011-01-13T10:55:56Z"
id: 870061
url: "https://tts.iteye.com/blog/870061"
---

随便写的，所以大家随便看看：  
利用Rectangle的contains或者containsPoint方法，得知鼠标是否在Rectangle所表示的矩形范围内；

 

```
//rollover指示ROLL_OVER事件，ROLL_OUT事件，rect用于检测的Rectangle，overPoint用于检测MOUSE_MOVE事件
var rollover : Boolean = false;
var rect : Rectangle = new Rectangle(0,0,200,300);
var overPoint : Point;        
addEventListener(Event.ENTER_FRAME, enterFrameHandler);

private function enterFrameHandler(event : Event) : void {
                        var mousePoint : Point = new Point(stage.mouseX, stage.mouseY);
                        if(rect.containsPoint(mousePoint)) {
                        //鼠标进入rect范围
                                if(!rollover){
                                        //鼠标首次进入rect
                                        rollover = true;
                                        overPoint = mousePoint;
                                        trace("ROLL_OVER");
                                }else{
                                        //鼠标在rect内移动
                                        if(overPoint && Point.distance(overPoint, mousePoint) != 0){
                                                overPoint = mousePoint;
                                                trace("MOUSE_MOVE");
                                        }
                                }
                        }else{
                         //鼠标移出rect范围
                                if(rollover){
                                //鼠标首次移出rect
                                        rollover = false;
                                        trace("ROLL_OUT");
                                }
                        }
                }
```