Main.as:

Код AS3:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
/**
* ...
* @author Hauts
*/
public class Main extends Sprite
{
private var _balls:Array;
private var _totalBalls:int;
private var _stageRect:Rectangle;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
stage.scaleMode = StageScaleMode.NO_SCALE
stage.showDefaultContextMenu = false;
stage.align = StageAlign.TOP_LEFT
stage.addEventListener(Event.RESIZE, stageResizeHandler);
stageResizeHandler(null)
_balls = [];
_totalBalls = 100;
var newBall:Ball;
for (var k:int = 0 ; k < _totalBalls; k ++ ) {
newBall = new Ball(10 + (Math.random() * 10), 2 + (Math.random() * 18));
newBall.x = _stageRect.left + Math.random() * _stageRect.width;
newBall.y = _stageRect.top + Math.random() * _stageRect.height;
_balls.push(addChild(newBall));
}
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function stageResizeHandler(e:Event):void
{
_stageRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
}
private function enterFrameHandler(e:Event):void
{
for (var k:int = 0 ; k < _totalBalls; k++ ) {
_balls[k].update(_stageRect);
}
}
}
}
Ball.as:

Код AS3:
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.geom.Rectangle;
/**
* ...
* @author Hauts
*/
public class Ball extends Sprite
{
public var radius:Number;
private var _vx:Number;
private var _vy:Number;
public function Ball(radius:Number, speed:Number = 20)
{
this.radius = radius
var randomAngle:Number = Math.PI * 2 * Math.random();
_vx = Math.cos(randomAngle) * speed;
_vy = Math.sin(randomAngle) * speed;
var gr:Graphics = this.graphics;
gr.beginFill(0xFFFFFF * Math.random())
gr.lineStyle(1);
gr.drawCircle(0, 0, radius);
gr.endFill()
gr.moveTo(0, 0);
gr.lineTo(radius, 0);
}
public function update(rect:Rectangle):void
{
x += _vx;
y += _vy;
if (x > rect.right - radius) {
_vx *= -1;
x = rect.right - radius;
} else if (x < rect.left + radius) {
_vx *= -1;
x = rect.left + radius;
}
if (y > rect.bottom - radius) {
_vy *= -1
y = rect.bottom - radius;
} else if (y < rect.top + radius) {
_vy *= -1
y = rect.top + radius
}
rotation = x
}
public function constrain(rect:Rectangle):void
{
x = Math.max(rect.left + radius, Math.min(rect.right - radius, x));
y = Math.max(rect.top + radius, Math.min(rect.bottom - radius, y));
}
}
}