In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

mouseEventの練習

MouseEventを複数設定する練習用に作ってみました。
赤玉をドラックして赤玉を投げるfunction(onBallMouse~)と
赤玉以外をドラックすると赤玉がマウスカーソルを追いかけるfunction(onFrameMouse~)作成。
Get Adobe Flash player
by kaiho 13 Nov 2009
    Embed
/**
 * Copyright kaiho ( http://wonderfl.net/user/kaiho )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3Hg6
 */

//MouseEventを複数設定する練習用に作ってみました。
//赤玉をドラックして赤玉を投げるfunction(onBallMouse~)と
//赤玉以外をドラックすると赤玉がマウスカーソルを追いかけるfunction(onFrameMouse~)作成。
package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    [SWF(width=400, height=400, backgroundColor=0x0, frameRate=120)]
	
    public class FlashTest extends Sprite{
        //ボール
        public var ball:Sprite;
        //ボールの半径
        public var ballR:Number = 50;
        //ボールの横方向の移動量
        public var vx:Number = 0;
        //ボールの縦方向の移動量
        public var vy:Number = 0;
		//壁接触時の跳ね返り
		public var hop:Number = -1;
		//ドラッグ中のマウスのX,Y座標
		public var tempX:Number;
		public var tempY:Number;
		
        public function FlashTest(){
            init();
        }

        public function init():void{
            //ボールの作成
            ball = new Sprite();
            ball.graphics.beginFill(0xff0000);  //塗りつぶし開始
            ball.graphics.drawCircle(0, 0, ballR);  //座標(0, 0)に半径ballRの円
            ball.graphics.endFill();  //塗りつぶし終了
			
            //ボールの位置指定(画面の真ん中)
            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;
			
			//ボールのイベント
 			ball.addEventListener(MouseEvent.MOUSE_DOWN, onBallMouseDown);
			ball.addEventListener(MouseEvent.MOUSE_UP, onBallMouseUp);
			//ボールを画面に追加
			addChild(ball);
			
			//stage上のイベント
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onFrameMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onFrameMouseUp)
			
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(event:Event):void{
            //速度に従ってボールの移動
            ball.x += vx;
            ball.y += vy;
			
            //画面のはみ出しチェック
            checkWall();
			
			//摩擦係数(だんだん遅く)
			vx *= 0.98;
			vy *= 0.98;
			
			//微妙に動き続けるのを防止
			if(vx < 0.001 && vx > -0.001) {
			    vx = 0;
			}
			
			if(vy < 0.001 && vy > -0.001) {
			    vy = 0;
			}
        }

		//マウスドラックのAction
		private function onMouseDrag(event:Event):void {
			vx = (mouseX - ball.x) * 0.1;
			vy = (mouseY - ball.y) * 0.1;
			
            ball.x += vx;
            ball.y += vy;
            
            //画面のはみ出しチェック
            checkWall();
		}

        private function checkWall():void{
            //横方向のチェック
            if(ball.x < ballR){
                ball.x = ballR;
                vx *= hop;
            }else if(stage.stageWidth - ballR < ball.x){
                ball.x = stage.stageWidth - ballR;
                vx *= hop;
            }
			
            //縦方向のチェック
            if(ball.y < ballR){
                ball.y = ballR;
                vy *= hop;
            }else if(stage.stageHeight - ballR < ball.y){
                ball.y = stage.stageHeight - ballR;
                vy *= hop;
            }
        }
		
		private function onBallMouseDown(event:MouseEvent):void{
			//ボールの運動を停止させる
			vx = 0;
			vy = 0;
			
			//ドラッグ中はボールの移動処理をしない
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			//マウスカーソル引き付けアクションの停止
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, onFrameMouseDown);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onFrameMouseUp);
			
			//ドラッグ中にマウスの座標を保存する処理を登録
			addEventListener(Event.ENTER_FRAME, onDragEnter);
			ball.startDrag();
		}

		private function onBallMouseUp(event:MouseEvent):void{
			//ボールの移動処理を再開
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			//マウスカーソル
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onFrameMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onFrameMouseUp);
			
			//マウスの座標を保存する処理を除去
			removeEventListener(Event.ENTER_FRAME, onDragEnter);
			
			//ボールを放り投げるX速度とY速度を設定
			vx = mouseX - tempX;
			vy = mouseY - tempY;
			
			ball.stopDrag();
		}
		
		private function onFrameMouseDown(mevnt:MouseEvent):void {

			//通常のball移動処理を停止
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			//ボールドラック時のアクションを停止
			ball.removeEventListener(MouseEvent.MOUSE_DOWN, onBallMouseDown);
			ball.removeEventListener(MouseEvent.MOUSE_UP, onBallMouseUp);
			//マウスカーソルの追っかけを開始
			addEventListener(Event.ENTER_FRAME, onMouseDrag);
		}
		
		private function onFrameMouseUp(evnt:MouseEvent):void {
			//マウスカーソルの追っかけを停止
			removeEventListener(Event.ENTER_FRAME, onMouseDrag);
			
			//通常のball移動処理を再開
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			//ボールドラック時のアクション再開
			ball.addEventListener(MouseEvent.MOUSE_DOWN, onBallMouseDown);
			ball.addEventListener(MouseEvent.MOUSE_UP, onBallMouseUp);
		}
		
		//ボールの移動方向をランダムで動かす
		private function randMove():void {
            //ボールの移動角度設定
            var radian:Number = Math.random() * 2 * Math.PI;
			//ボールの速度設定
			var v:Number = Math.random() * 100;
            //角度の要素からx方向、y方向の移動量を取得
            vx = Math.cos(radian) * v;
            vy = Math.sin(radian) * v;
		}
		
		//ドラック中の座標の取得
		private function onDragEnter(event:Event):void{
			tempX = mouseX;
			tempY = mouseY;
		}
    }
}