02. マウスイベント

by yooyke forked from 01. 繰り返し (ENTER_FRAME) (diff: 46)
マウスイベントあれこれ
Clickも使えるが、MOUSE_UP と組み合わせると難しい
♥0 | Line 49 | Modified 2010-11-30 16:44:46 | MIT License
play

ActionScript3 source code

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

// forked from nextyukke's 01. ENTER_FRAME Event
package {
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        private var tf_:TextField;
        private var rect_:Sprite;
        
        public function FlashTest() {
            // write as3 code here..
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event=null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            // 文字を表示できるようにする
            tf_ = new TextField();
            tf_.mouseEnabled = false;
            tf_.width = stage.stageWidth;
            tf_.height = stage.stageHeight;
            addChild(tf_);

            // オブジェクト
            rect_ = new Sprite();
            rect_.graphics.beginFill(0x00ff00);
            rect_.graphics.drawRect(0, 0, 100, 100);
            rect_.graphics.endFill();
            rect_.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); // マウスのボタンを押したときに呼ばれる
            rect_.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver); // マウスのカーソルがオブジェクトの上に来た時に呼ばれる
            rect_.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); // マウスのカーソルがオブジェクトの上で動いた時に呼ばれる
            rect_.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);     // マウスのボタンが押された状態から放したときに呼ばれる
            rect_.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);   // マウスのカーソルがオブジェクトから離れたときに呼ばれる
            rect_.x = 300;
            rect_.y = 100;
            addChild(rect_);
        }
        
        private function onMouseDown(e:MouseEvent):void {
            tf_.text = "Called onMouseDown";
        }

        private function onMouseOver(e:MouseEvent):void {
            tf_.text = "Called onMouseOver";
        }
        
        private function onMouseMove(e:MouseEvent):void {
            tf_.text = "Called onMouseMove";
        }
        
        private function onMouseUp(e:MouseEvent):void {
            tf_.text = "Called onMouseUp";
        }
        
        private function onMouseOut(e:MouseEvent):void {
            tf_.text = "Called onMouseOut";
        }
    }
}