flash on 2009-11-23

by osamX
removeChildはするけど、Arrayにはnullを代入するだけ。
* Array.lengthはどんどん長くなるけど、これ大丈夫なの?テスト
♥2 | Line 69 | Modified 2009-11-23 11:42:28 | MIT License
play

ActionScript3 source code

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

/**
 * removeChildはするけど、Arrayにはnullを代入するだけ。
 * Array.lengthはどんどん長くなるけど、これ大丈夫なの?テスト
 */

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import net.hires.debug.Stats;
	
	public class Test extends Sprite 
	{
		private const NUM:uint = 20;
		private var particles:Array = [];
		private var fragments:Array = [];
		
		public function Test():void {
			addChild(new Stats());
			//stage.addEventListener(MouseEvent.CLICK, createFragment);
			//var s:Sprite = new Sprite();
			//s.graphics.beginFill(0x000000);
			//s.graphics.drawCircle(465/2, 465/2, 100);
			//s.graphics.endFill();
			//addChild(s);
			var tf:TextField = new TextField();
			tf.width = 465;
			tf.x = 70;
			addChild(tf);
			
			addEventListener(Event.ENTER_FRAME, function(e:Event):void {
				tf.text = "particles.length = " + String(particles.length);
				createFragment();
				for (var i:uint = 0; i < particles.length; i++) {
					var p:Particle = particles[i];
					if(p && p.y < 500){
						p.vy += 1.5;
						p.vx *= 0.987;
						p.vy *= 0.987;
						p.x += p.vx;
						p.y += p.vy;
						fragments[i].x = p.x;
						fragments[i].y = p.y;
					}else if(fragments[i]){
						removeChild(fragments[i]);
						fragments[i] = null;
						particles[i] = null;
					}
				}
			});
		}
		
		private function createFragment(e:MouseEvent = null):void {
			var l:uint = fragments.length;
			for (var i:uint = 0; i < NUM; i++) {
				var p:Particle = new Particle(465/2, 465/2, 40 * Math.random()-20, -20 * Math.random());
				particles.push(p);
				
				var s:Sprite = new Sprite();
				//s.graphics.lineStyle(5, 0x330000);
				//s.graphics.beginFill(0xCC6633);
				s.graphics.beginFill(0xFFFFFF*Math.random());
				s.graphics.drawCircle(0, 0, 10);
				s.graphics.endFill();
				s.x = p.x;
				s.y = p.y;
				fragments.push(s);
				addChildAt(fragments[l + i],0);
			}
		}	
	}
}

class Particle 
{
	public var x:Number;
	public var y:Number;
	public var vx:Number;
	public var vy:Number;
	
	public function Particle(_x:Number=0, _y:Number=0,_vx:Number=0,_vy:Number=0):void {
		x = _x;
		y = _y;
		vx = _vx;
		vy = _vy;
	}
}