TechHUB:Flash1:04:MouseEvent

by yprops
♥0 | Line 78 | Modified 2011-03-26 17:42:25 | MIT License
play

ActionScript3 source code

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

package {
    import flash.text.TextField;
    import flash.display.*;
    import flash.events.MouseEvent;
    public class FlashTest extends Sprite {
        
        /*
        イベントオブジェクト・・・手紙
        イベント      ・・・手紙の内容「マウスクリックが起こったよ!」
        イベントリスナー  ・・・手紙の受け取り手
        イベントハンドラー ・・・受け取った後のリスナーの行動「じゃあ描画を変えよう」
        
        ※バブリング ・・・今回範囲外 表示リスト上のイベントの振る舞い 理解するととても便利
        */
        
        
        //変数-------------------------------
        private var count :int;
        private var myText :TextField;
        private var ositene :Sprite;
        
        
        //コンストラクタ-----------------------
        public function FlashTest() {
            count = 0;
            initOutput();
            makeOsitene();
        }
        
        
        //メイン処理--------------------------
        private function makeOsitene() :void{
            var i :int;
            var sp :Sprite;
            
            ositene = new Sprite();
            addChild(ositene);
            ositene.x = 100;
            ositene.y = 200;
            
            // Spriteインスタンスに イベントリスナー設定
            ositene.addEventListener(
                MouseEvent.CLICK,
                onOsiteneClick
            );
            // Spriteインスタンスに イベントリスナー設定
            ositene.addEventListener(
                MouseEvent.MOUSE_MOVE,
                onOsiteneMouseMove
            );
            
            
            for( i = 0; i < 10; i++){    //繰り返し
                sp = makeSprite();
                sp.x = Math.random() * 100 - 50;
                sp.y = Math.random() * 100 - 50;
                ositene.addChild(sp);
            }
            count++;
        }
        private function removeOsitene() :void{
            // Spriteインスタンスからイベントリスナーを削除
            // インスタンスが不要になった時、イベントリスナーを削除してから捨てないとメモリ上に残って動き続けることがある。
            ositene.removeEventListener(MouseEvent.CLICK, onOsiteneClick);
            ositene.removeEventListener(MouseEvent.MOUSE_OVER, onOsiteneMouseMove);
            
            removeChild(ositene);
            ositene = null;
        }
        
        
        //イベントハンドラメソッド---------------------
        private function onOsiteneClick(event:MouseEvent) :void{
            removeOsitene();
            makeOsitene();
            renewOutput(event.type);
        }
        private function onOsiteneMouseMove(event:MouseEvent) :void{
            if(event.shiftKey == true){
                ositene.x = ositene.x - 1;
            }else{
                ositene.x = ositene.x + 1;
            }
            

            if(ositene.x > 300){
                ositene.x = 100;
            }
            
            renewOutput(event.type);
        }
        
        
        //その他メソッド-----------------------
        
        private function makeSprite() :Sprite{
            var sp :Sprite = new Sprite();
            var gra :Graphics = sp.graphics;
            gra.beginFill(0, 0.5);
            gra.drawRect(-50, -50, 100, 100);
            gra.endFill();
            return sp;
        }

        private function initOutput() :void{
            myText = new TextField();
            myText.width = 400;
            myText.height = 40;
            myText.border = true;
            addChild(myText);
        }
        private function renewOutput(message :String) :void{
            myText.text = "再生回数:" + count + "\n" + message;
        }

        
    }
}

Forked