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

Tiling Hexagon 六角模様

Get Adobe Flash player
by mtok 10 Jan 2009
package  
{
	import flash.text.TextField;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;

	import flash.utils.getQualifiedClassName;
	
	[SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="0")]
	public class HexaTail extends Sprite
	{	
		private var output:TextField;

		public function HexaTail()
		{
			initOutput();
			println('Construct ' + getQualifiedClassName(this));
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		
		public function init():void {
			
			graphics.beginFill(0xBF927A);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			
			graphics.beginFill(0xF2D2B6);
			fillHexagon(18, 3, 15, 9);
			graphics.endFill();

			graphics.beginFill(0xBF927A);
			fillHexagon(16, 5, 15, 9);
			graphics.endFill();

			graphics.beginFill(0xF2D2B6);
			fillHexagon(9, 12, 15, 9);
			graphics.endFill();
			
		}
		private function fillHexagon(r:Number, space:Number, cx:int, cy:int):void {
			var cos30:Number = Math.cos(Math.PI / 6);
			var dist:Number = (2 * (r + space) * cos30);
			
			var i:int;
			for (var j:int = 0; j < cy; j++) {
				for (i = 0; i < cx; i++) {
					drawHexagon(r, i * dist, j * 2 * dist * cos30);
				}
				for (i = 0; i < cx; i++) {
					drawHexagon(r, (i + 0.5) * dist, (j * 2 + 1) * dist * cos30);
				}
			}
		}
		
		private function drawHexagon(r:Number, x:Number = 0, y:Number = 0):void
		{
			var th:Number = Math.PI / 6;
			var cos:Number = r * Math.cos(th);
			var sin:Number = r * Math.sin(th);
			graphics.moveTo(x + 0, y - r);
			graphics.lineTo(x + cos, y -sin);
			graphics.lineTo(x + cos, y + sin);
			graphics.lineTo(x + 0, y + r);
			graphics.lineTo(x - cos, y + sin);
			graphics.lineTo(x - cos, y - sin);
			graphics.lineTo(x, y - r);
		}
		

		private function addedToStage(e:Event):void 
		{
			removeEventListener(e.type, arguments.callee);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			init();	
		}
		
		////////////////
		// 出力
		///////////////
		private function initOutput():void {
			var margin:Number = 10;
			
			addChild(output = new TextField());
			output.selectable = false;
			output.multiline = true;
			output.width = stage.stageWidth - margin * 2;	
			output.height = stage.stageHeight - margin * 2;	
		}
		public function print(... args):void {
			output.appendText(args.join(", "));
		}
		public function println(... args):void {
			output.appendText(args.join(", "));
			output.appendText("\n");
		}
	}
}