REMOVE BALL

by kazy
Main
メインクラス.
♥0 | Line 63 | Modified 2009-06-02 23:37:45 | MIT License
play

ActionScript3 source code

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

package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	/**
	 * Main
	 * メインクラス.
	 */
	public class Main extends Sprite {
		private var count:int = 100;
		private var balls:Array;
		/**
		 * コンストラクタ.
		 */
		public function Main() {
			init();
		}
		/**
		 * 初期化.
		 */
		private function init():void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			balls = new Array();
			for (var i:int = 0; i < count; i++) {
				var ball:Ball = new Ball();
				ball.x = Math.random() * stage.stageWidth;
				ball.y = Math.random() * stage.stageHeight;
				ball.vx = Math.random() * 5 - 1;
				ball.vy = Math.random() * 5 - 1;
				addChild(ball);
				balls.push(ball);
			}
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		/**
		 * onEnterFrame.
		 */
		private function onEnterFrame(event:Event):void {
			trace("working");
			for (var i:Number = balls.length - 1; i > -1; i--) {
				var ball:Ball = Ball(balls[i]);
				ball.x += ball.vx;
				ball.y += ball.vy;
				//ボールがステージ外に出たら消す
				if (ball.x - ball.radius > stage.stageWidth ||
				   ball.x + ball.radius < 0 ||
				   ball.y - ball.radius > stage.stageHeight ||
				   ball.y + ball.radius < 0) {
					removeChild(ball);
					balls.splice(i, 1);
					//ボールが全て消えたらonEnterFrameも消す
					if (balls.length <= 0) {
						removeEventListener(Event.ENTER_FRAME, onEnterFrame);
						trace("stoped");
					}
				}

			}
		}
	}
}
/**
 * Ball
 * ボール生成クラス.
 */
class Ball extends flash.display.Sprite {
	public var radius:Number;
	private var color:uint;
	public var vx:Number = 0;
	public var vy:Number = 0;
	/**
	 * コンストラクタ.
	 */
	public function Ball(radius:Number=5, color:uint=0x990033) {
		this.radius = radius;
		this.color = color;
		init();
	}
	/**
	 * 初期化.
	 */
	public function init():void {
		graphics.beginFill(color);
		graphics.drawCircle(0, 0, radius);
		graphics.endFill();
	}
}

Forked