flash on 2010-9-26

by tepe
マウスイベントの処理
♥0 | Line 52 | Modified 2010-10-01 15:17:20 | 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/sApY
 */

package {
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    import flash.ui.*;
    import flash.utils.*;
    
    //マウスイベントの処理
   [SWF(width=240, height=240, backgroundColor=0xFFFFFF)]
    public class MouseEx extends Sprite {
        private var Label     :TextField;      //ラベル
        private var mouseDown :String = "";//マウスダウン
        private var mouseDelta:int    = 0;     //マウスホイール
        

        //コンストラクタ
        public function MouseEx() {
            //ベースの生成
            var base:Sprite = new Sprite();            
            base.graphics.beginFill(0xffffff);            
            base.graphics.drawRect(0, 0, 240, 240);            
            base.graphics.endFill();
            addChild(base);

            //ラベルの追加
            Label = makeLabel("マウスイベント");            
            base.addChild(Label);
            
            //イベントリスナーの追加
            base.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);            
            base.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);            
            base.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);            

            //タイマーの追加
            var timer:Timer = new Timer(1000/60, 0);            
            timer.addEventListener(TimerEvent.TIMER, onTick);//イベントリスナーに実行する関数を追加            
            timer.start();//タイマーイベント開始
        }

        //ラベルの生成
        private function makeLabel(text:String):TextField {            
            var label:TextField = new TextField();            
            label.text       = text;            
            label.autoSize   = TextFieldAutoSize.LEFT;            
            label.selectable = false;            
            return label;
        }

        //タイマーイベントの処理
        private function onTick(evt:TimerEvent):void {
            var text:String = "";            
            text += "マウスダウン:" + mouseDown + "\n";            
            text += "マウス座標:" + Math.floor(mouseX) + "," + Math.floor(mouseY) + "\n";            
            text += "マウスホイール:" + mouseDelta + "\n";
            text += mouseX;            
            Label.text = text;            
        }   
        
        //マウスダウンイベントの処理
        private function onMouseDown(evt:MouseEvent):void {
            
            mouseDown = "on";            
        }

        //マウスアップイベントの処理
        private function onMouseUp(evt:MouseEvent):void {
            mouseDown = "off";            
        }
    
        //マウスホイールイベントの処理
        private function onMouseWheel(evt:MouseEvent):void {
            mouseDelta += evt.delta;            
        }
    }
}