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

[セルフフォーク]ひだまりスケッチ ほしみっつ の星 噴水バージョン

ひだまりスケッチ ほしみっつ の星 を飛び散るようにしてみた 噴水バージョン
@author Hiiragi

セルフフォーク その2

その1と同じように、「オブジェクトプール」を試してみた感じです。

最初に720個の星と初期設定をしてるので、最初若干重いです。

rotationさせたら一気に重くなったので、
星の一番外側の白枠を消すことで、一応少しパフォーマンスアップ。
package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.net.FileReference;
	import flash.utils.Timer;
	
	import com.bit101.components.PushButton;
	
	/**
	 * ひだまりスケッチ ほしみっつ の星 を飛び散るようにしてみた 噴水バージョン
	 * @author Hiiragi
	 * 
	 * セルフフォーク その2
	 * 
	 * その1と同じように、「オブジェクトプール」を試してみた感じです。
	 * 
	 * 最初に720個の星と初期設定をしてるので、最初若干重いです。
	 * 
	 * rotationさせたら一気に重くなったので、
	 * 星の一番外側の白枠を消すことで、一応少しパフォーマンスアップ。
	 */
	
	[SWF(width = 465, height = 465, frameRate = 60, backgroundColor = 0xFFFFFF)]
	public class Main2 extends Sprite 
	{
		
		public function Main2():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			start();
		}
		
		private function start():void 
		{
			
			var mtx:Matrix = new Matrix();
			mtx.createGradientBox(this.stage.stageWidth, this.stage.stageHeight, Math.PI / 2);
			var g:Graphics = this.graphics;
			g.beginGradientFill(GradientType.LINEAR, [0xFFEEEE, 0xFFFFFF], [1, 1], [0, 255], mtx);
			g.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
			g.endFill();
			
			var fountain:HidamariStarFountain = new HidamariStarFountain(this.stage.stageWidth, this.stage.stageHeight);
			this.addChild(fountain);
		}
	}
	
}



import adobe.utils.ProductManager;
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.Matrix;
import flash.geom.Point;

class HidamariStarFountain extends Sprite
{
	private const STAR_MAX:uint = 720;
	private const BURST_NUM:int = 60;
	
	private var _sw:int;
	private var _sh:int;
	
	private var _starPool:Array;
	private var _activeStars:Array;
	
	private var _gravity:Number = 0.98;
	private var _fountain:Point;
	
	public function HidamariStarFountain(stageWidth:int, stageHeight:int)
	{
		_sw = stageWidth;
		_sh = stageHeight;
		
		_fountain = new Point(_sw / 2, _sh + 100);
		
		_starPool = [];
		_activeStars = [];
		
		var star:HidamariStarExtend;
		var absX:Number;
		var absY:Number;
		
		for (var i:int = 0; i < STAR_MAX; i++) 
		{
			star = new HidamariStarExtend(Math.random() * 40 + 10);
			
			star.x = -2000;
			star.y = -2000;
			
			this.addChild(star);
			_starPool[i] = star;
		}
		
		this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
	}
	
	private function onEnterFrameHandler(e:Event):void 
	{
		var star:HidamariStarExtend = _starPool.shift();
		star.x = _fountain.x;
		star.y = _fountain.y;
		star.vx = Math.random() * 30 - 15;
		star.vy = -35;
			
		_activeStars.push(star);
		
		var len:int = _activeStars.length;
		
		var deleteFlag:Boolean;
		
		for (var i:int = len - 1; i >= 0; i--) 
		{
			deleteFlag = false;
			
			star = _activeStars[i];
			star.vy += _gravity;
			star.x += star.vx;
			star.y += star.vy;
			star.rotation += 10;
			
			if (star.x < -star.width / 2)
			{
				deleteFlag = true;
			}
			else if (star.x > _sw + star.width / 2)
			{
				deleteFlag = true;
			}
			
			if (star.y < -star.height / 2)
			{
				deleteFlag = true;
			}
			else if (star.y > _sh + star.height / 2 + 150)
			{
				deleteFlag = true;
			}
			
			if (deleteFlag)
			{
				_activeStars.splice(i, 1);
				_starPool.push(star);
				star.x = -2000;
				star.y = -2000;
			}
		}
	}
}

class HidamariStar extends Sprite
{
	private var _cx:Number;
	private var _cy:Number;
	private var _radius:Number;
	private var _insideRatio:Number;
	
	private var _secondStar:Sprite;
	private var _centerStar:Sprite;
	
	private var _secondStarBevelFilter:BevelFilter;
	private var _centerStarBevelFilter:BevelFilter;
	
	public function HidamariStar(radius:Number,
								 cx:Number = 0, 
								 cy:Number = 0, 
								 insideRatio:Number = 0.5) 
	{
		_radius = radius;
		_cx = cx;
		_cy = cy;
		_insideRatio = insideRatio;
		
		_secondStar = new Sprite();
		_centerStar = new Sprite();
		this.addChild(_secondStar);
		this.addChild(_centerStar);
		
		_secondStarBevelFilter = new BevelFilter(2, -45, 0x996666, 1, 0, .3, 2, 2, 1, 2, BitmapFilterType.OUTER, false);
		_centerStarBevelFilter = new BevelFilter(2, -45, 0x996666, 1, 0, .5, 2, 2, 1, 2, BitmapFilterType.OUTER, false);
		_secondStar.filters = [_secondStarBevelFilter];
		_centerStar.filters = [_centerStarBevelFilter];
		
		draw();
	}
	
	public function draw():void 
	{
		var mtx:Matrix = new Matrix();
		
		var g:Graphics = this.graphics;
		//g.clear();
		
		//g.beginFill(0xFFFFFF);
		//g.lineStyle(10, 0xFFFFFF);
		//drawStar(g, _radius);
		//g.endFill();
		
		g = _secondStar.graphics;
		g.clear();
		mtx.createGradientBox(_radius * 2, _radius * 2, -Math.PI / 4, -_cx -_radius, -_cy - _radius);
		g.beginGradientFill(GradientType.LINEAR, [0xf0ac96, 0x780100], [1, 1], [0, 255], mtx);
		drawStar(g, _radius * .95);
		g.endFill();
		
		g = _centerStar.graphics;
		g.clear();
		mtx.createGradientBox(_radius * 2, _radius * 2, -Math.PI / 4, -_cx -_radius, -_cy - _radius);
		g.beginGradientFill(GradientType.LINEAR, [0xfdf9fa, 0xfdae10], [1, 1], [0, 255], mtx);
		drawStar(g, _radius * 0.8);
		g.endFill();
		
	}
	
	private function drawStar(g:Graphics, radius:Number):void
	{
		g.moveTo(_cx, _cy - radius);
		
		var diffRad:Number = Math.PI * 2 / 10;
		var rad:Number = -Math.PI / 2;
		var tempRadius:Number;
		
		for (var i:int = 1; i <= 10; i++)
		{
			rad += diffRad;
			tempRadius = (i % 2 == 1) ? tempRadius = radius * _insideRatio : tempRadius = radius;
			g.lineTo(Math.cos(rad) * tempRadius + _cx, Math.sin(rad) * tempRadius + _cy);
		}
	}
	
	public function get radius():Number { return _radius; }
	
	public function set radius(value:Number):void 
	{
		_radius = value;
		draw();
	}
	
	public function get insideRatio():Number { return _insideRatio; }
	
	public function set insideRatio(value:Number):void 
	{
		_insideRatio = value;
		draw();
	}
	
}


class HidamariStarExtend extends HidamariStar
{
	private var _vx:Number;
	
	public function get vx():Number { return _vx; }
	
	public function set vx(value:Number):void 
	{
		_vx = value;
	}
	
	private var _vy:Number;
	public function get vy():Number { return _vy; }
	
	public function set vy(value:Number):void 
	{
		_vy = value;
	}
	
	public function HidamariStarExtend(radius:Number,
								 cx:Number = 0, 
								 cy:Number = 0, 
								 insideRatio:Number = 0.5) 
	{
		super(radius, cx, cy, insideRatio);
		this.mouseEnabled = false;
	}
}