
Код AS1/AS2:
var stageWidth = 1024;
var stageHeight = 768;
var speed = 10;
var ball = createBall();
var randomStartAngle = Math.random() * Math.PI * 2;
ball.vx = Math.cos(randomStartAngle);
ball.vy = Math.sin(randomStartAngle);
ball._x = stageWidth / 2;
ball._y = stageHeight / 2;
function createBall() {
var d = this.getNextHighestDepth();
var ballInstance = this.createEmptyMovieClip("ball_" + d, d);
ballInstance.beginFill(0x000000,100);
ballInstance.moveTo(-10,-10);
ballInstance.lineTo(10,-10);
ballInstance.lineTo(10,10);
ballInstance.lineTo(-10,10);
ballInstance.lineTo(-10,-10);
ballInstance.endFill();
return ballInstance;
}
this.onEnterFrame = function() {
var newPosX = ball._x + ball.vx * speed;
var newPosY = ball._y + ball.vx * speed;
if (newPosX < 0 || newPosX > stageWidth) {
ball.vx *= -1;
}
if (newPosY < 0 || newPosY > stageHeight) {
ball.vy *= -1;
}
ball._x += ball.vx * speed;
ball._y += ball.vy * speed;
};