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

forked from: flash on 2009-5-22

まだまだ調整中~
Get Adobe Flash player
by hacker_f4482ykb 01 Oct 2009
/**
 * Copyright hacker_f4482ykb ( http://wonderfl.net/user/hacker_f4482ykb )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3Sgq
 */

// forked from muu's flash on 2009-5-22
//まだまだ調整中~

package{   
  import flash.display.Sprite;                
  import flash.text.TextField; 
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.display.*;
  import flash.utils.setInterval;
  import flash.utils.Timer;
  import flash.events.TimerEvent;
  import flash.geom.Point; 

  public class Main extends Sprite {   

    public function Main() { 

	stage.scaleMode = StageScaleMode.NO_SCALE; //スケールモード変更
	stage.align = StageAlign.TOP_LEFT; //座標基準を左上に

	var xx:int = 0; //画面はしのマージン
	var yy:int = 0; //画面はしのマージン
	var mg:int = 50; //MC間のマージン
	var cnt:int = 60; //最大MC数
	var v:int = 3; //移動速度設定
	var w : int = stage.stageWidth;
	var h : int = stage.stageHeight;
	var clickStart:int = 0;//MCのクリック可否フラグ
	var zoom:int = 0;//クリックアクションフラグ
	var zp:int;//ズーム倍率

	var target_naviX:Number;//スタイルボタン移動先座標X
	var targetx:Array = new Array();//MC移動先座標X
	var targety:Array = new Array();//MC移動先座標Y
	var target_moveX:Number;//スライドMC移動先座標X
	var target_moveY:Number;//スライドMC移動先座標Y

	var style:int = 1;//レイアウトボタン宣言
	var t_mc:Array = new Array(); //MC格納変数
	var s_btn:Array = new Array();

	//メインMC生成
	var bg_main:Sprite = new Sprite();
	//スライドMC生成
	var bg_move:Sprite = new Sprite();
	//MCフィールド生成
	var bg_fld:Sprite = new Sprite();
	bg_fld.graphics.drawRect(0, 0, 0, 0);

	stage.addChild(bg_main);
	bg_main.addChild(bg_move);
	bg_move.addChild(bg_fld);

	//MC作成
	var i:int;
	for(i = 1 ; i <= cnt ; i++){	
		var mc:Sprite = new Sprite();
		//var sc:Sprite = new Sprite();
		mc.graphics.beginFill(0x000000);
		mc.graphics.drawRect(0, 0, 50, 50);
		mc.graphics.endFill();
		bg_fld.addChild(mc);

		var mcText:TextField = new TextField();
		mcText.textColor = 0xFFFFFF;
		mcText.text = String(i);
		mcText.autoSize = "left";
		mcText.x = mc.width/2 - mcText.width/2;
		mcText.y = mc.height/2 - mcText.height/2;
		mc.addChild(mcText);

		t_mc[i] = mc;//生成したMCを変数に代入
		t_mc[i].addEventListener(MouseEvent.ROLL_OVER , rOver);
		t_mc[i].addEventListener(MouseEvent.ROLL_OUT , rOut);
		t_mc[i].addEventListener(MouseEvent.CLICK , mcClick);

		t_mc[i].buttonMode = true;
		t_mc[i].mouseChildren = false; //MC内の子要素を無視
	}

	//-------------------------------------------------
	
	//レイアウトボタン生成
	var navi:Sprite = new Sprite();
	stage.addChild(navi);

	var q:int;
	for(q = 1 ; q < 4 ; q++){	
		var btn:Sprite = new Sprite();
		btn.graphics.beginFill(0x000000);
		btn.graphics.drawRect(0, 0, 30, 30);
		btn.graphics.endFill();
		navi.addChild(btn);
		s_btn[q] = btn;
		s_btn[q].buttonMode = true;

		if(q != 1){
			s_btn[q].x = s_btn[q-1].x + 50;
		}
		s_btn[q].addEventListener(MouseEvent.CLICK , s_change);
	}

	//-------------------------------------------------

	grid();//初期スタイル設定

	//-------------------------------------------------

	stage.addEventListener(Event.RESIZE, s_select);

	function s_select(event:Event):void{
		switch(style){
			case 1 :
				grid(); break;
			case 2 :
				grid2(); break;
			case 3 :
				grid3(); break;
		}
	}

	//-------------------------------------------------

	//MCマウスアクション
	function rOver(event:MouseEvent):void{
		event.target.alpha = 0.5;
	}
	function rOut(event:MouseEvent):void{
		event.target.alpha = 1;
	}
	//ボタンマウスアクション
	function s_change(event:MouseEvent):void{
		switch(event.target){
			case s_btn[1] :
				style = 1; grid(); break;
			case s_btn[2] :
				style = 2; grid2(); break;
			case s_btn[3] :
				style = 3; grid3(); break;
		}
	}

	//-------------------------------------------------

	function grid():void{
		xx = 10;
		yy = 10;
		var w : int = stage.stageWidth;
		var h : int = stage.stageHeight;
		var i:int;

		for(i = 1 ; i <= cnt ; i++){
			//リサイズ時の移動先座標の指定
			targetx[i] = xx;
			targety[i] = yy;
			xx = xx  + mg;
	
			//横幅いっぱい時の処理
			if ( xx > w-50){
				//trace("over"+","+ root["mc"+i].name );
				targetx[i] = xx = 10;
				targety[i] = yy + mg;
				yy += mg; //改行する
				xx += mg;
			}
		}
		target_naviX = w /2 - navi.width/2;
	};

	function grid2():void{
		xx = 10;
		yy = 10;
		var w : int = stage.stageWidth;
		var h : int = stage.stageHeight;
		var i:int;
	
		for(i = cnt ; i >= 0 ; i--){
			//リサイズ時の移動先座標の指定
			targetx[i] = xx;
			targety[i] = yy;
			xx = xx  + mg;
		
			//横幅いっぱい時の処理
			if ( xx > w-50){
				//trace("over"+","+_root["mc"+i] );
				targetx[i] = xx = 10;
				targety[i] = yy + mg;
				yy += mg; //改行する
				xx += mg;
			}
		}
		target_naviX = w /2 - navi.width/2;
	};

	function grid3():void{
		xx = 10;
		yy = 10;
		var w : int = stage.stageWidth;
		var h : int = stage.stageHeight;
		var i:int;
	
		for(i = 0 ; i <= cnt ; i++){
			//リサイズ時の移動先座標の指定
			targetx[i] = Math.floor( Math.random()*- (w-w/2)+(w-w/2));
			targety[i] = Math.floor( Math.random()*- (h-h/2)+(h-h/2));
		}
		target_naviX = w /2 - navi.width/2;
	};

	//-------------------------------------------------

	//移動先座標へ移動
	addEventListener(Event.ENTER_FRAME , s_move);

	function s_move(event:Event):void {

		//初期配置設定
		bg_main.x = stage.stageWidth/2 - bg_main.width/2;
		bg_main.y = stage.stageHeight/2 - bg_main.height/2;
		bg_main.alpha = 0.8;
		if (zoom == 0){
			var i:int;
			for(i = 1 ; i <= cnt ; i++){
				moveMC(t_mc[i] , targetx[i] , targety[i]);
			}
			moveMC(navi , target_naviX , 10);
			bg_fld.x = bg_move.width/2 - bg_fld.width/2;
			bg_fld.y = bg_move.height/2 - bg_fld.height/2;
		}
	}
	//MCクリック用関数
	function mcClick(event:Event):void{
		var scale_tx:Number = bg_main.width/2;  //拡大の中心座標
		var scale_ty:Number = bg_main.height/2;  
		var move_tx:Number;
		var move_ty:Number;

		if(clickStart == 0){	//拡大処理未実行時のみ
			event.target.addEventListener(Event.ENTER_FRAME , zoomEnterFrame);
			clickStart = 1;
		
			if (zoom){
				move_tx =  0;
			move_ty =  0;
					zp = 1;//ズーム倍率
				//zoom = 0;
			}else{
				move_tx = bg_main.width/2 - bg_move.width/2 -( event.target.x - bg_move.width/2)- event.target.width/2;
				move_ty = bg_main.height/2 - bg_move.height/2 -(event.target.y - bg_move.height/2)- event.target.height/2;
				zp = 3;//ズーム倍率
				zoom = 1;
			}
		}

		function zoomEnterFrame(event:Event):void{

			if (Math.abs(move_tx - bg_move.x)>0.18 ||  Math.abs(move_ty - bg_move.y)>0.18 ){

				//移動用MCの移動
				  bg_move.x += (move_tx - bg_move.x)/v;
				  bg_move.y += (move_ty - bg_move.y)/v;
				//中心座標のグローバル座標を取得
				  var mcl_pos:Point = new Point(scale_tx, scale_ty);
				  var mcg_pos:Point = bg_main.localToGlobal(mcl_pos);
				//拡大処理
				  bg_main.scaleX += (zp - bg_main.scaleX)/v; 
				  bg_main.scaleY += (zp - bg_main.scaleY)/v;
				//拡大後の中心座標のグローバル座標を取得
				  var afl_pos:Point = new Point(scale_tx, scale_ty);
				  var afg_pos:Point = bg_main.localToGlobal(afl_pos);
				//拡大前後の座標のズレを補正
				  bg_main.x += mcg_pos.x - afg_pos.x;
				  bg_main.y += mcg_pos.y - afg_pos.y ;

			}else{
				bg_move.x = move_tx;
				bg_move.y = move_ty;
				bg_main.scaleX = bg_main.scaleY = zp;
				event.target.removeEventListener(Event.ENTER_FRAME,zoomEnterFrame);
				clickStart = 0;
				if(zoom && zp == 1){
					zoom = 0;
				}
			}
		}
	}

	function moveMC(target_mc:Sprite, tx:Number, ty:Number):void {
			if (Math.abs(tx - target_mc.x)>0.18 ||  Math.abs(ty - target_mc.y)>0.18 ){
				target_mc.x += (tx - target_mc.x)/v;
				target_mc.y += (ty - target_mc.y)/v;
			}else{
				target_mc.x = tx;
				target_mc.y = ty;
				clickStart = 0;
			}
			if(zoom && zp == 1){
				zoom = 0;
			}
	}
    } 
  }
}