Circle bounce, random : flash on 2010-2-13

by hazel_eyes
♥0 | Line 43 | Modified 2010-02-13 13:11:45 | MIT License
play

ActionScript3 source code

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

package {
	import flash.display.Sprite;
	import flash.events.Event;

	public class moveCircle extends Sprite {
		public var _Array:Array = new Array();

		public function moveCircle() {
			for (var i:int = 0; i<30; i++) {
				var _circle:Object = new Object();
				_circle.sp = new Sprite();
				_circle.clr = Math.random()*0xFFFFFF;
				_circle.cSize = Math.random()*20 + 5;
				_circle.xVel = Math.random()*2*20-20;
				_circle.yVel = Math.random()*2*20-20;
				_circle.initX = Math.random()*stage.stageWidth;
				_circle.initY = Math.random()*stage.stageHeight;
				_Array.push(_circle);

				cDraw(_Array[i].sp, _Array[i].clr, _Array[i].initX, _Array[i].initY, _Array[i].cSize);
				
				addChild(_Array[i].sp);
				this.addEventListener(Event.ENTER_FRAME, frameMove);
			}
		}
		public function frameMove(e:Event):void{
			for(var i:int = 0; i<30; i++){
				_Array[i].sp.x += _Array[i].xVel;
				_Array[i].sp.y += _Array[i].yVel;
				if(_Array[i].sp.x > stage.stageWidth || _Array[i].sp.x < 0){
					_Array[i].xVel *= -1;
				}
				if(_Array[i].sp.y > stage.stageHeight || _Array[i].sp.y < 0){
					_Array[i].yVel *= -1;
				}
			}
		}		
		public function cDraw(sp:Sprite, clr:int, cx:Number, cy:Number, size:Number):void{
			sp.graphics.beginFill(clr);
			sp.graphics.drawCircle(0, 0, size);
			sp.graphics.endFill();
			sp.x = cx;
			sp.y = cy;
			sp.alpha = 0.3;			
		}
	}
}