Beautifl - Flash Gallery

Thumbnail : 画面を分割
画面を分割
HaraMakoto 2009-08-30 MIT License

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	[SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="40")]
	public class GridClock2 extends Sprite
	{
		private var gridArray:Array;
		private var gridNum:int = 40;
		
		private var maxW:Number = 465;
		private var maxH:Number = 465;
		
		private var counter:int = 0;
		
		private var first:Boolean = true;
		
		private var txt:TextField = new TextField();
		
		//すでに追加されている四角形の配列
		private var addedRectArray:Array = new Array();
		
		public function GridClock2()
		{
			addChild(txt);
			makeRects();
			addEventListener(Event.ADDED_TO_STAGE,addStageHanlder);
		}
		
		private function makeRects():void
		{
			gridArray = new Array(gridNum);
			for(var i:int=0; i<gridNum; i++) {
				var rct:GridUnit = new GridUnit();
				gridArray[i] = rct;
				rct.myNum = i;
				rct.col = 0xFFCC00*Math.random();
				addChild(rct);
			}
		}
		
		private function addStageHanlder(e:Event):void
		{
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.textColor = 0x000000;
			txt.mouseEnabled = false;
			txt.text = "クリックで画面分割"
			txt.x = stage.stageWidth / 2 - txt.width/2;
			txt.y = stage.stageHeight / 2;
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		private function clickHandler(e:MouseEvent):void
		{
			txt.visible = false;
			if(counter < gridNum){
				addRect();
			}
			
		}
		
		/**
		 * 四角形を1つ追加、開始点とゴール点を決めます。
		 * 開始点とゴール点をもとに、他の四角形の各頂点のx値、あるいはy値が全体の何%の位置にあるかを計算、
		 * ゴール地点から逆の端点の間の、同じ割合の点を他の四角形のゴール地点に設定。
		 * 
		 */
		//四角形を追加、開始点とゴール地点を決めます
		private function addRect():void
		{
			var grect:GridUnit = gridArray[ counter ];
			//開始点、ゴール点
			var startPt1:Point = new Point(0,0);
			var startPt2:Point = new Point(0,0);
			var startPt3:Point = new Point(0,0);
			var startPt4:Point = new Point(0,0);
			var goalPt1:Point = new Point(0,0);
			var goalPt2:Point = new Point(0,0);
			var goalPt3:Point = new Point(0,0);
			var goalPt4:Point = new Point(0,0);
			
			var pptp:String = "top";
			var numm:Number = Math.random();
			
			var goalX:Number = maxW * (0.5);
			var goalY:Number = maxH * (0.5);
			
			if(numm<0.25) {
				pptp = "right";
				if(first) {
					goalX = 0;
					goalY = 0;
					first = false;
				}
			} else if(numm<0.5){
				pptp = "left";
				if(first) {
					goalX = 0;
					goalY = maxH;
					first = false;
				}
			} else {
				pptp = "top";
				if(first) {
					goalX = 0;
					goalY = maxH;
					first = false;
				}
			}
			
			
			if(pptp=="right") {
				//右からの場合
				startPt1.x = maxW; startPt1.y = 0; startPt2.x = maxW; startPt2.y = 0;
				startPt3.x = maxW; startPt3.y = maxH; startPt4.x = maxW; startPt4.y = maxH;
				//ゴールのx地点を設定
				//var leftNum:Number = maxW*Math.random();
				var leftNum:Number = goalX;
				goalPt1.x = leftNum; goalPt1.y = 0; goalPt2.x = maxW; goalPt2.y = 0;
				goalPt3.x = maxW; goalPt3.y = maxH; goalPt4.x = leftNum; goalPt4.y = maxH;
				//他の点のゴール計算
				culcGoal(goalPt1, pptp);
			} else if(pptp=="left") {
				//左からの場合
				startPt1.x = 0; startPt1.y = 0; startPt2.x = 0; startPt2.y = 0;
				startPt3.x = 0; startPt3.y = maxH; startPt4.x = 0; startPt4.y = maxH;
				//ゴールのx地点を設定
				var rightNum:Number = maxW-goalX;
				goalPt1.x = 0; goalPt1.y = 0; goalPt2.x = rightNum; goalPt2.y = 0;
				goalPt3.x = rightNum; goalPt3.y = maxH; goalPt4.x = 0; goalPt4.y = maxH;
				//他の点のゴール計算
				culcGoal(goalPt3, pptp);
			} else {
				//上からの場合
				startPt1.x = 0; startPt1.y = 0; startPt2.x = maxW; startPt2.y = 0;
				startPt3.x = maxW; startPt3.y = 0; startPt4.x = 0; startPt4.y = 0;
				//ゴールのy地点を設定
				var topNum:Number = goalY;
				goalPt1.x = 0; goalPt1.y = 0; goalPt2.x = maxW; goalPt2.y = 0;
				goalPt3.x = maxW; goalPt3.y = topNum; goalPt4.x = 0; goalPt4.y = topNum;
				//他の点のゴール計算
				culcGoal(goalPt3, pptp);
			}
			
			
			
			//今の四角のスタート、ゴール設定
			grect.setStart(startPt1, startPt2, startPt3, startPt4);
			grect.setGoals(goalPt1, goalPt2, goalPt3, goalPt4);
			
			//四角形描画
			grect.moveToGoal();
			gotoGoalRects();
			
			addedRectArray.push(grect); //表示中の配列に追加
			counter++;
		}
		//既に配置されている四角形をゴールまで移動します
		private function gotoGoalRects():void
		{
			var leng:int = addedRectArray.length;
			for(var i:int=0; i<leng; i++) {
				addedRectArray[i].moveToGoal();
			}
		}
		//開始点とゴール点をもとに、配置されているrectのゴール点を計算
		private function culcGoal(gPt:Point, posT:String):void{
			var leng:int = addedRectArray.length;
			var pointOfGoalNum:Number;
			var tempGoal1:Point = new Point(0,0);
			var tempGoal2:Point = new Point(0,0);
			var tempGoal3:Point = new Point(0,0);
			var tempGoal4:Point = new Point(0,0);
			//右からの場合 || maxWに対しての各点の割合出す || 0からゴール点に対しての割合の座標をゴールに
			
			var i:int; var gu:GridUnit; var per1:Number; var per2:Number; var per3:Number; var per4:Number;
			if(posT=="right"){
				pointOfGoalNum = gPt.x;
				for( i=0; i<leng; i++) {
					gu = addedRectArray[i];
					per1 = gu.pt1.x / maxW;
					per2 = gu.pt2.x / maxW;
					per3 = gu.pt3.x / maxW;
					per4 = gu.pt4.x / maxW;
					trace("per1=="+per1);
					tempGoal1.x = pointOfGoalNum*per1; tempGoal1.y = gu.pt1.y;
					tempGoal2.x = pointOfGoalNum*per2; tempGoal2.y = gu.pt2.y;
					tempGoal3.x = pointOfGoalNum*per3; tempGoal3.y = gu.pt3.y;
					tempGoal4.x = pointOfGoalNum*per4; tempGoal4.y = gu.pt4.y;
					gu.setGoals(tempGoal1,tempGoal2,tempGoal3,tempGoal4);
				}
			//左からの場合 || maxWに対しての各点の割合出す || ゴール点からmaxWに対しての割合+ゴール点の座標をゴールに
			} else if(posT=="left") {
				pointOfGoalNum = maxW - gPt.x;
				for( i=0; i<leng; i++) {
					gu = addedRectArray[i];
					per1 = gu.pt1.x / maxW;
					per2 = gu.pt2.x / maxW;
					per3 = gu.pt3.x / maxW;
					per4 = gu.pt4.x / maxW;
					tempGoal1.x = pointOfGoalNum*per1+gPt.x; tempGoal1.y = gu.pt1.y;
					tempGoal2.x = pointOfGoalNum*per2+gPt.x; tempGoal2.y = gu.pt2.y;
					tempGoal3.x = pointOfGoalNum*per3+gPt.x; tempGoal3.y = gu.pt3.y;
					tempGoal4.x = pointOfGoalNum*per4+gPt.x; tempGoal4.y = gu.pt4.y;
					gu.setGoals(tempGoal1,tempGoal2,tempGoal3,tempGoal4);
					
				}
			//上からの場合 || maxHに対しての各点の割合出す || ゴール点からmaxHに対しての割合+ゴール点の座標をゴールに
			} else if(posT=="top") {
				pointOfGoalNum = maxH - gPt.y;
				for( i=0; i<leng; i++) {
					gu = addedRectArray[i];
					per1 = gu.pt1.y / maxH;
					per2 = gu.pt2.y / maxH;
					per3 = gu.pt3.y / maxH;
					per4 = gu.pt4.y / maxH;
					tempGoal1.x = gu.pt1.x; tempGoal1.y = pointOfGoalNum*per1+gPt.y;
					tempGoal2.x = gu.pt2.x; tempGoal2.y = pointOfGoalNum*per2+gPt.y;
					tempGoal3.x = gu.pt3.x; tempGoal3.y = pointOfGoalNum*per3+gPt.y;
					tempGoal4.x = gu.pt4.x; tempGoal4.y = pointOfGoalNum*per4+gPt.y;
					gu.setGoals(tempGoal1,tempGoal2,tempGoal3,tempGoal4);
				}
				
			}
		}
	}
}
	import flash.display.MovieClip;
	import flash.geom.Point;
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	

class GridUnit extends MovieClip
{
	public var myNum:int;
	public var wNum:int=1;
	public var hNum:int=1;
	public var xposNum:Number=0;
	public var yposNum:Number=-1;
	public var _name:String;
	public var pt1:Point = new Point(0,0);
	public var pt2:Point = new Point(0,0);
	public var pt3:Point = new Point(0,0);
	public var pt4:Point = new Point(0,0);
	public var Goalpt1:Point = new Point(0,0);
	public var Goalpt2:Point = new Point(0,0);
	public var Goalpt3:Point = new Point(0,0);
	public var Goalpt4:Point = new Point(0,0);
	public var col:Number = 0x000000;
	//表示オブジェクト
	private var MC:MovieClip = new MovieClip();
	private var MCw:Number; private var MCh:Number;
	
	public function GridUnit() {
		addChild(MC);
		MC.graphics.clear();
		MC.graphics.beginFill(0xFFFFFF);
		MC.graphics.drawCircle(0,0, 10);
		//MC.graphics.drawRect(-100,-50,200,100);
		MC.graphics.endFill();
		MCw = MC.width; MCh = MC.height;
	}
	public function makeRect():void
	{
		var mar:Number = 2;
		this.graphics.clear();
		this.graphics.beginFill(col);
		this.graphics.moveTo(pt1.x+mar, pt1.y+mar);
		this.graphics.lineTo(pt2.x-mar, pt2.y+mar);
		this.graphics.lineTo(pt3.x-mar, pt3.y-mar);
		this.graphics.lineTo(pt4.x+mar, pt4.y-mar);
		this.graphics.endFill();
		setScalePos(); //配置オブジェクトを調整
	}
	//ゴールへ向かってTween
	public function moveRect(_pt1:Point, _pt2:Point, _pt3:Point, _pt4:Point):void
	{
		
	}
	
	//配置オブジェクトのスケールと位置調整
	private function setScalePos():void
	{
		MC.x = pt1.x+(pt2.x-pt1.x)/2;
		MC.y = pt1.y+(pt3.y-pt1.y)/2;
		//縦幅と横幅
		var dx:Number = pt2.x - pt1.x;
		var dy:Number = pt3.y - pt1.y;
		//はみだし大きい方優先
		var fitNum:Number; var scW:Number; var scH:Number;
		MC.width==0 ? scW=1 : scW=MC.width;
		MC.height==0 ? scH=1 : scH=MC.height;
		//(dx-MC.width > dy-MC.height)?fitNum=dx/scW : fitNum=dy/scH;
		(dx-MCw < dy-MCh)?fitNum=dx/MCw : fitNum=dy/MCh;
		//fitNum=dx/scW;
		MC.scaleX = MC.scaleY = fitNum;
	}
	//開始点の設定
	public function setStart(_pt1:Point, _pt2:Point, _pt3:Point, _pt4:Point):void
	{
		pt1.x = _pt1.x; pt1.y = _pt1.y;
		pt2.x = _pt2.x; pt2.y = _pt2.y;
		pt3.x = _pt3.x; pt3.y = _pt3.y;
		pt4.x = _pt4.x; pt4.y = _pt4.y;
		makeRect();
	}
	
	//ゴールの設定
	public function setGoals(_pt1:Point, _pt2:Point, _pt3:Point, _pt4:Point):void
	{
		var mar:Number = 0;
		Goalpt1.x = _pt1.x+mar; Goalpt1.y = _pt1.y+mar;
		Goalpt2.x = _pt2.x-mar; Goalpt2.y = _pt2.y+mar;
		Goalpt3.x = _pt3.x-mar; Goalpt3.y = _pt3.y-mar;
		Goalpt4.x = _pt4.x+mar; Goalpt4.y = _pt4.y-mar;
	}
	
	//保持しているゴール座標に向かってTween
	public function moveToGoal():void
	{
		var trans:String = "easeOutExpo";
		var _time:Number = 0.5;
		Tweener.addTween(pt1,{x:Goalpt1.x, y:Goalpt1.y, transition:trans, time:_time});
		Tweener.addTween(pt2,{x:Goalpt2.x, y:Goalpt2.y, transition:trans, time:_time});
		Tweener.addTween(pt3,{x:Goalpt3.x, y:Goalpt3.y, transition:trans, time:_time});
		Tweener.addTween(pt4,{x:Goalpt4.x, y:Goalpt4.y, transition:trans, time:_time, onUpdate:makeRect});
	}
	
	//配置オブジェクトのスケールに反映させる処理
	private function setMCScale():void
	{
		
	}
	
}