forked from: PentagonalStar++ by click

by umhr forked from PentagonalStar++ by click (diff: 28)
♥0 | Line 64 | Modified 2010-04-29 14:49:16 | MIT License
play

ActionScript3 source code

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

// forked from simultechnology's PentagonalStar++ by click

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Main extends Sprite
	{
		private var _arrayStar:Array = [];
		private var _arrayRotation:Array = [];
		
		public function Main()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
		
		/* 
		* 初期処理メソッド
		*/
		private function init(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			// 文字列フォーマットの指定
			var tm:TextFormat = new TextFormat();
			tm.bold = true;
			tm.font = "Gothic";
			tm.size = 20;
			// 文字列の作成
			var tf:TextField = new TextField();
			tf.defaultTextFormat = tm;
			tf.width = 300;
			tf.text = "More Click\nanywhere!!";
			tf.x = stage.stageWidth/2;
			tf.y = stage.stageHeight / 2;
			tf.mouseEnabled = false;
			this.stage.addChild(tf);
			
			// 一つ目の星を作成
			//var star:Sprite = new Sprite();
			createStar(200, 200, 50);
			this.stage.addEventListener(MouseEvent.CLICK, goToCreateStar, false, 0, true);
			
			addEventListener(Event.ENTER_FRAME, rotate, false, 0, true);
		}
		
		/*
		* 2つ目以降の星を作成するメソッド
		*/
		private function goToCreateStar(e:MouseEvent):void
		{
			//var star:Sprite = new Sprite();
			createStar(mouseX, mouseY, 100 * (Math.random() * 0.5 + 0.5));
		}
		
		/*
		* 星を描写するメソッド
		*/
		private function createStar(x:int, y:int, radius:int):void
		{	
			var star:Sprite = new Sprite();
			var i:int;
			star.graphics.lineStyle(2, (Math.random() * 0.9 + 0.1) * 0xffffff, Math.random() * 0.5 + 0.5);
			star.x = x;
			star.y = y;
			star.graphics.moveTo(radius * Math.cos(72 * Math.PI / 180), radius * Math.sin(72 * Math.PI / 180));
			// 順番に角度72の倍数ずつ、順番に頂点をずらしていくと正五角形になるが、ここで欲しいのは
			// 星型なので、頂点を一つ飛ばしでラインを書いていく。なのでfor文のインクリメントは2の倍数
			for (i = 1; i < 6; i ++)
			{
				star.graphics.lineTo(radius * Math.cos((72 + 144 * i) * Math.PI / 180 ), radius * Math.sin((72 + 144 * i) * Math.PI / 180));
			}
			/*
			// 上記の理由によりfor文のインクリメントは奇数
			for (i = 1; i < 6; i += 2)
			{
				star.graphics.lineTo(radius * Math.cos((72 + 72 * i) * Math.PI / 180 ), radius * Math.sin((72 + 72 * i) * Math.PI / 180));	
			}
			*/
			// 回転度数を設定(-5度 ~ 5度)
			this._arrayRotation.push(Math.random() >= 0.5 ? (Math.random() * 0.9 + 0.1) * 5 : (Math.random() * -0.9 - 0.1) * 5);
			// 作成した星を配列に追加
			this._arrayStar.push(star);
			star.mouseEnabled = false;
			
			//星ひとつごとにENTER_FRAMEを走らせるの?
			//addEventListener(Event.ENTER_FRAME, rotate, false, 0, true);		
			addChild(star);
		}
		
		private function rotate(e:Event):void
		{
			//forで毎回lengthをとりに行くのは高コストなので、
			//一回ですむようにする。
			var n:int = this._arrayStar.length;
			for (var i:int = 0; i < n; i++) {
				this._arrayStar[i].rotation += this._arrayRotation[i];
			}
		}
	}
}