カーソルのステージへの出入りを検出

by tepe
♥0 | Line 35 | Modified 2011-01-24 13:07:53 | MIT License
play

ActionScript3 source code

/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/w2uU
 */

package {  
  
    import flash.display.*;  
    import flash.events.*;  
      
    import flash.text.TextField;  
    import flash.text.TextFormat;  
    import flash.text.TextFieldAutoSize;  
    import flash.text.TextFormatAlign;  
      
    public class Sample20091104 extends Sprite {  
          
        private var _text:TextField;  
          
        public function Sample20091104() {
            
            _text= new TextField();  
            _text.width = 160;  
            _text.autoSize = TextFieldAutoSize.CENTER;  //autoSizeを指定しないと高さが100pxになる  
            _text.x = stage.stageWidth / 2 - _text.width / 2;  
            _text.y = stage.stageHeight / 2 - _text.height / 2;  
            _text.selectable=false;  
            addChild(_text);  
              
            var format:TextFormat=new TextFormat();  
            format.color=0x333333;  
            format.size = 12;  
            _text.defaultTextFormat = format;  
              
            _text.text = "マウスカーソルはステージ外";  
            
            
            //カーソルのステージへの出入りを検出
            stage.addEventListener(Event.MOUSE_LEAVE, stageOut);
            stage.addEventListener(MouseEvent.MOUSE_MOVE,stageIn);
            
              
        }  
        
        //カーソルがステージ外に出た
        private function stageOut(e:Event):void {  
            _text.text = "マウスカーソルはステージ外";  
            stage.addEventListener(MouseEvent.MOUSE_MOVE, stageIn);    
        }  
        
        //カーソルがステージ内にある
        private function stageIn(e:MouseEvent):void {  
            stage.removeEventListener(MouseEvent.MOUSE_MOVE,stageIn);
            _text.text = "マウスカーソルはステージ内";      
        }  
          
    }  
}