Ive got a rough version of it working but im sure theres a better way to do it. Idealy id like it to happen randomly (say if a variable that changed everytime the circles moved was greater than a certain number).
Heres the way i got it working:
import flash.display.Sprite;
import flash.events.Event;
var circArray :Array = new Array()
var circXArray :Array = new Array()
var circX :int = 5
var circY :int = 5
var circCount :int = 300
var difFlip :int = 500
var div :int = 3
var j = 0
var k = 0
function drawCircs() {
for (var i = 0; i < circCount; i ++) {
var circ :Sprite = new Sprite();
var fill :int = 0xffffff * Math.random();
var alfa :Number = 0.99 * Math.random();
var x0 :int = 400;
var y0 :int = 600;
var radius :int = 20;
circArray.push(circ = new Sprite()); //puts the circle sprite in an Array
addChild(circArray);
circArray.graphics.lineStyle(1);
circArray.graphics.beginFill(fill, alfa);
circArray.graphics.drawCircle(x0, y0, radius);
circArray.graphics.endFill();
circArray.addEventListener(Event.ENTER_FRAME, move)
circXArray.push(circX)
j += 1
}
}
function move (evt:Event) { //moves every circle sprite individually
var thisCirc = evt.currentTarget
var flip = Math.random() * difFlip
if (flip < difFlip / div) {
circXArray = -circXArray
}
thisCirc.x += circXArray
thisCirc.y -= circY
k += 1
if (k == circCount) {
k = 0
}
}
I dont know how to add a preview of the program like you did either
Hi again.
I tried your approach but all the circles just kept moving in the same direction while shaking a bit.
Also, for better performance:
- Use vectors instead of arrays;
- Randomize the position of the object itself not the drawing parameters because it will be much harder for you to check for collisions, position the objects precisely, and so on;
- Try to always use only one enterFrame event for the sake of maintenance and performance;
- You used the new keyword when creating the circ object so there's no need to use it again when pushing this object to the array.
Here is an updated code.
The shapes now move based on their rotation values. At every 2 seconds each shape change its direction. Notice that you can uncomment three lines and change the object to the arrow I used below.
Main code:
import flash.display.DisplayObjectContainer;
import flash.utils.setInterval;
//var objs:Vector.<Arrow> = new Vector.<Arrow>();
var objs:Vector.<Circ> = new Vector.<Circ>();
var interval:uint;
const DEGREES:Number = Math.PI / 180;
function generateObjects(total:uint, container:DisplayObjectContainer):void
{
for (var i:uint = 0; i < total; i++)
{
//var obj:Arrow = new Arrow();
var obj:Circ = new Circ();
obj.x = stage.stageWidth * Math.random();
obj.y = stage.stageHeight;
obj.rotation = -180 + Math.random() * 180;
obj.direction = obj.rotation;
obj.speedX = 2 + Math.random() * 3;
obj.speedY = 2 + Math.random() * 3;
obj.friction = 0.85;
container.addChild(obj);
objs.push(obj);
}
}
function enterFrameHandler(event:Event):void
{
for (var i:uint = 0, total:uint = objs.length; i < total; i++)
{
//var obj:Arrow = objs;
var obj:Circ = objs;
if (obj.x < -obj.width * 0.5)
obj.x = stage.stageWidth + obj.width * 0.5;
else if (obj.x > stage.stageWidth + obj.width * 0.5)
obj.x = -obj.width * 0.5;
if (obj.y < -obj.height * 0.5)
obj.y = stage.stageHeight + obj.height * 0.5;
else if (obj.y > stage.stageHeight + obj.height * 0.5)
obj.y = -obj.height * 0.5;
obj.rotation = lerp(obj.rotation, obj.direction, obj.friction);
obj.x = obj.x + obj.speedX * Math.cos(obj.rotation * DEGREES);
obj.y = obj.y + obj.speedY * Math.sin(obj.rotation * DEGREES);
}
}
function changeDirection():void
{
for (var i:uint = 0, total:uint = objs.length; i < total; i++)
objs.direction = -180 + Math.random() * 180;
}
function lerp(v1:Number, v2:Number, f:Number):Number
{
return f * v1 + (1 - f) * v2;
}
generateObjects(25, this);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
interval = setInterval(changeDirection, 2000);
Circ.as code:
package
{
import flash.display.Sprite;
public dynamic class Circ extends Sprite
{
public function Circ()
{
graphics.lineStyle(1);
graphics.beginFill(0xffffff * Math.random(), 0xffffff * Math.random());
graphics.drawCircle(0, 0, 20);
graphics.endFill();
}
}
}
Preview:
(The preview is just a GIF. Please ignore the framerate.)

FLA download: animate_cc_as3_move_shapes.zip - Google Drive .
Please don't hesitate to ask if you have any further questions.
Regards,
JC