/////Mouse in Stage check ///////// //// author:chenlangeer //// msn:chenlangeer@hotmail.com ////email:chenlangeer@eastday.com //////////////////////////////////// import flash.geom.*; class MouseCheck { private static var rect:Rectangle; private static var out:Boolean; private static var m_pos:Point; private static var old_m_pos:Point; private static var out_val:Number; private static var _val:Number; private static var out_func:Function; private static var in_func:Function; private static var frame:Number; private static var border:Number; private static function onResize() { rect = new Rectangle(border, border, Stage.width-border*2, Stage.height-border*2); //trace(rect); } public static function init(_frame:Number, out_f:Function, in_f:Function) { Stage.scaleMode = "noScale"; Stage.align = "TL"; Stage.addListener(MouseCheck); border = 1; ///////// rect = new Rectangle(border, border, Stage.width-border*2, Stage.height-border*2); m_pos = new Point(_root._xmouse, _root._ymouse); frame = 1000/_frame; out_func = out_f; in_func = in_f; ///////// if (!rect.containsPoint(m_pos)) { out_func(); out = true; } else { in_func(); out = false; } } private static function cheekOut() { old_m_pos = m_pos.clone(); m_pos = new Point(_xmouse, _ymouse); if (!m_pos.equals(old_m_pos)) { var n_pos:Point = new Point(m_pos.x+(m_pos.x-old_m_pos.x)*1.1, m_pos.y+(m_pos.y-old_m_pos.y)*1.1); if (rect.containsPoint(n_pos)) { if (out) { out = false; in_func(); trace("mouse move in screen"); } if (out_val) { clearInterval(out_val); } } else { clearInterval(out_val); delete out_val; out_val = setInterval(timeOut, frame*3); } } } private static function timeOut() { var old_d = m_pos.clone(); m_pos = new Point(_xmouse, _ymouse); if (m_pos.equals(old_d) && !out) { out = true; out_func(); trace("mouse move out screen"); } clearInterval(out_val); delete out_val; } public static function startListen() { trace("start listen"); clearInterval(_val); delete _val; _val = setInterval(cheekOut, frame); } public static function clearListen() { clearInterval(_val); delete _val; } function MouseCheck() { } }