(some buggy version, the problem shows after a while...)
code on scene timeline, frame 1:
//task07
//define an object of "circle"
//make 3 instances of it and give them different properties
//let them run and collide with each other and with the
walls
//set the initial values for the 3 instances of "circle"
object
//blueBall, redBall, and greenBall
function initCircle() {
@@blueBall.setColor(0x0066ff);
@@blueBall.xVelocity=1;
@@blueBall.yVelocity=1;
@@redBall.setColor(0xff3333);
@@redBall.setScale(85);
@@redBall.xVelocity=-3;
@@redBall.yVelocity=4;
@@greenBall.setColor(0x66cc33);
@@greenBall.setScale(70);
@@greenBall.xVelocity=5;
@@greenBall.yVelocity=-8;
}
//calculations
needed to be done for updating frame
function frameUpdate() {
@@//detect if two balls are
colliding
@@//if so, swap velocity
and color
@@if (blueBall.distanceTo(redBall)<=blueBall.sumRadiusWith(redBall)
@@@@@@//this
is to see if two balls are too close
@@@@@@&&
blueBall.distanceTo(redBall)<blueBall.distanceOld(redBall)){
@@@@@@//this
is to see if they have just collided in last frame
@@@@blueBall.swapVelWith(redBall);
@@@@blueBall.swapColorWith(redBall);
@@}
@@if (blueBall.distanceTo(greenBall)<=blueBall.sumRadiusWith(greenBall)
@@@@@@&&
blueBall.distanceTo(greenBall)<blueBall.distanceOld(greenBall)){
@@@@blueBall.swapVelWith(greenBall);
@@@@blueBall.swapColorWith(greenBall);
@@}
@@if (redBall.distanceTo(greenBall)<=redBall.sumRadiusWith(greenBall)
@@@@@@&&
redBall.distanceTo(greenBall)<redBall.distanceOld(greenBall)){
@@@@redBall.swapVelWith(greenBall);
@@@@redBall.swapColorWith(greenBall);
@@}
@@//update the position of
the balls
@@blueBall.updatePosition();
@@redBall.updatePosition();
@@greenBall.updatePosition();
}
//do
something at each frame
blueBall.onEnterFrame = frameUpdate;
//init the movie when load
this.onLoad = initCircle;
code on movieClip "circle", layer "code", frame 1:
////"circle" - object's properties
//give
the default values
this.xVelocity=0;//the distance it'll go in each frame
(in x/y direction)
this.yVelocity=0;
this.xOld=0;//the x/y position of it in last frame
this.yOld=0;
//more are system-defined properties
//such as _x, _y, _xscale, _yscale, etc.
//I
don't know what this does, just copied from Pete's "canvas" code.
:)
this.circle = _root.circle;
////"circle" - object's functions
//print
out something in the output window, for debugging
this.printOut = function(whichIns, positionX, positionY) {
@@trace("instance of " + whichIns + "()
: " + positionX);
@@trace("instance of " + whichIns + "()
: " + positionY);
};
//set
color for an instance
this.setColor = function(iColor){
@@var newColor=new Color(this);
@@newColor.setRGB(iColor);
};
//swap
color with another instance
this.swapColorWith = function(ball){
@@var ballColor=new Color(ball);
@@var thisColor=new Color(this);
@@var bColor=ballColor.getRGB();
@@ballColor.setRGB(thisColor.getRGB());
@@thisColor.setRGB(bColor);
}
//set
scale for an instance
this.setScale = function(iScale){
@@this._xscale=iScale;
@@this._yscale=iScale;
}
//calculate
the distance with another ball (center to center)
function distanceTo(ball){
@@return Math.sqrt(Math.pow((this._x-ball._x),2)+Math.pow((this._y-ball._y),2));
};
//calculate
the distance for last frame
//(is used to avoid continuous swapping velocity when collide with another ball)
function distanceOld(ball){
@@return Math.sqrt(Math.pow((this.xOld-ball.xOld),2)+Math.pow((this.yOld-ball.yOld),2));
};
//calculate
the sum of the radius of the two balls about to collide
function sumRadiusWith(ball){
@@return this._xScale/100*36+ball.xScale/100*36;
};
//swap
velocity when two balls collide
//assume elastic collide, with same mass for each ball
function swapVelWith(ball){
@@var xVel=this.xVelocity;
@@var yVel=this.yVelocity;
@@this.xVelocity=ball.xVelocity;
@@this.yVelocity=ball.yVelocity;
@@ball.xVelocity=xVel;
@@ball.yVelocity=yVel;
};
//calculate
the position for the next frame, with the updated velocity
this.updatePosition=function(){
@@//if
the ball is collide with the walls, make them bounce back
@@var dist=this._xscale/100*36;
@@if (this._x>(200-dist/2) && this.xVelocity>0){
@@@@this.xVelocity=-this.xVelocity;
@@}
@@if (this._x<dist/2 && this.xVelocity<0){
@@@@this.xVelocity=-this.xVelocity;
@@}
@@if (this._y>(200-dist/2) && this.yVelocity>0){
@@@@this.yVelocity=-this.yVelocity;
@@}
@@if (this._y<dist/2 && this.yVelocity<0){
@@@@this.yVelocity=-this.yVelocity;
@@}
@@this.xOld=this._x;//keep
track of the old(last frame) position of the ball
@@this.yOld=this._y;
@@this._x+=this.xVelocity;//new
position with this velocity
@@this._y+=this.yVelocity;
@@//this.printOut(this,
this._x, this._y);
@@//this.printOut(this,
this.xVelocity, this.yVelocity);
@@//above
two lines: formerly used to debug
};
//stop
(don't know what it does exactly)
stop();