var totalBgObjects:Number = 3; var lastXOffset:Number = 15; var lastYOffset:Number = 15; for (var i:Number = 0; i < totalBgObjects; i++) { var obj:MovieClip = _level0.attachMovie('bgObject', 'bgObject' + String(i), i); obj.speedX = obj.speedY = randomBetween(i + 1, 5); obj._x = lastXOffset + obj._width + 20; obj._y = lastYOffset + obj._height + 20; lastXOffset = obj._x; lastYOffset = obj._y; obj.onEnterFrame = function():Void { for (var n:Number = 0; n < totalBgObjects; n++) { if (this != _level0['bgObject' + String(n)]) { if (_level0['bgObject' + String(n)].hitTest(this)) { this.speedX = this.speedX > 0 ? randomBetween(-5, -1) : randomBetween(1, 5); this.speedY = this.speedY > 0 ? randomBetween(-5, -1) : randomBetween(1, 5); } } } if (this._x - 2 <= this._width / 2) { this.speedX = this.speedX > 0 ? randomBetween(-5, -1) : randomBetween(1, 5); this._x += 2; } else if (this._x - 2 >= Stage.width - this._width / 2) { this.speedX = this.speedX > 0 ? randomBetween(-5, -1) : randomBetween(1, 5); this._x -= 2; } if (this._y - 2 <= this._height / 2) { this.speedY = this.speedY > 0 ? randomBetween(-5, -1) : randomBetween(1, 5); this._y += 2; } else if (this._y - 2 >= Stage.height - this._height / 2) { this.speedY = this.speedY > 0 ? randomBetween(-5, -1) : randomBetween(1, 5); this._y -= 2; } this._x += this.speedX; this._y += this.speedY; }; } // common functions function randomBetween(a:Number, b:Number):Number { return (a + Math.floor(Math.random() * (b - a + 1))); };