Groups | Blog | Home
all groups > flash actionscript > april 2007 >

flash actionscript : Random Moving Circles


Matt-R
4/13/2007 6:55:41 PM
I am trying animate the image in the supplied link.

Basically I want a bunch of circles to move around randomly inside a box. I
think I'd also like them to bounce of the edges of the box so that they are
moving in all directions.

The circles will be different sizes so I'm not sure if I need to apply code to
each circle size or if there is a scripting way to create randomly sized
circles.

Can anyone help me out, or point me in the direction of a good tutorial. I've
been animating in Flash for a while but just begining with action script.

Thanks!
http://home.graphics.uwaterloo.ca/testage/accounting/sample.jpg
kglad
4/13/2007 7:00:08 PM
Matt-R
4/13/2007 7:04:43 PM
No the circles don'l need to bounce off each other.
kglad
4/13/2007 7:25:19 PM
it would make it easier but i already have an experiment that i created where
the circles (more like billiard balls) bounce off each other and the table
edges.

i have it on my home computer so i'll check it in a few hours when i get home.
kglad
4/15/2007 12:00:00 AM
here's the code with balls bouncing off each other. i used four movieclips
boxleft, boxright, boxtop, boxbottom to form a rectangular border and there's a
ball movieclip with linkage id = ballID



// initialization parameters to set initail conditions **********
minSpeed = 2;
maxSpeed = 4;
ballNum = 9;
leftBorder = 10;
rightBorder = 500;
topBorder = 10;
bottomBorder = 380;
// end initializations **********************************
dep = 0;
tl = this;
for (var i = 1; i<=ballNum; i++) {
ballMC = tl.attachMovie("ballID", "ballMC"+i, dep++);
ballMC.tf.text = i;
ballMC.cacheAsBitmap = 1;
ballMC.speed = minSpeed+Math.random()*(maxSpeed-minSpeed);
ballMC.dir = -Math.PI+Math.random()*Math.PI*2;
ballMC.xVector = ballMC.speed*Math.cos(ballMC.dir);
ballMC.yVector = ballMC.speed*Math.sin(ballMC.dir);
ballMC._x = leftBorder+Math.random()*(rightBorder-leftBorder);
ballMC._y = topBorder+Math.random()*(bottomBorder-topBorder);
}
borderA = [boxtop, boxright, boxbottom, boxleft];
moveI = setInterval(moveF, 10);
function moveF() {
for (var j = 1; j<=ballNum; j++) {
ballMC = tl["ballMC"+j];
if (Math.abs(ballMC.xVector)<.1) {
ballMC.xVector = 0;
}
if (Math.abs(ballMC.yVector)<.1) {
ballMC.yVector = 0;
}
ballMC._x += ballMC.xVector;
ballMC._y += ballMC.yVector;
for (var k = 1; k<=ballNum; k++) {
if (k != j) {
ball2 = tl["ballMC"+k];
if (ballMC.hitTest(tl["ballMC"+k])) {
ballMC._x -= ballMC.xVector;
ballMC._y -= ballMC.yVector;
ball2._x -= ball2.xVector;
ball2._y -= ball2.yVector;
contactDir = Math.atan2(ball2._y-ballMC._y, ball2._x-ballMC._x);
ballMC.tempX = ballMC.xVector;
ballMC.tempY = ballMC.yVector;
ball2.tempX = ball2.xVector;
ball2.tempY = ball2.yVector;
ballMC.xVector +=
(ball2.tempX-ballMC.tempX)*Math.abs(Math.cos(contactDir));
ballMC.yVector +=
(ball2.tempY-ballMC.tempY)*Math.abs(Math.sin(contactDir));
ball2.xVector +=
(ballMC.tempX-ball2.tempX)*Math.abs(Math.cos(contactDir));
ball2.yVector +=
(ballMC.tempY-ball2.tempY)*Math.abs(Math.sin(contactDir));
}
}
}
for (var i = 0; i<borderA.length; i++) {
if (ballMC.hitTest(borderA[i])) {
ballMC._x -= ballMC.xVector;
ballMC._y -= ballMC.yVector;
if (i%2 == 0) {
ballMC.yVector = -ballMC.yVector;
} else {
ballMC.xVector = -ballMC.xVector;
}
}
}
}
updateAfterEvent();
}
Matt-R
4/16/2007 12:00:00 AM
Thanks for the code!
However I tried it and it's not working yet. I think it probably has something
to do with the Movie Clips I created.

Just to clarify, the things I need to add are:
- a ball MC called ballMC
- four MCs called boxleft, boxright, boxtop, boxbottom

Should I place all these MCs on the stage?
What are the names I should give each MC instance?

Hope this makes sense.
Thanks.
kglad
4/16/2007 2:30:05 PM
only the box movieclips are onstage to form the border encasing the balls. you
can set their _alpha to 0 if you don't want them visible.

but you need a ball moviecip with a LINKAGE ID = ballID. remove the ball
movieclip from the stage, find it in the library, right click it, click
linkage, tick export for actionscript and in the linkage id box type: ballID,
then click ok and retest.
AddThis Social Bookmark Button