Walled Bouncing Colorful Polygons Animation Using Flash And Action Script
Do you want to create an animation of twelve bouncing colorful polygons using Flash and Action Script. In this animation, twelve polygons will keep bouncing after an edge hit a wall. The area is a rectangle so that each of the polygons will keep bouncing inside the rectangle. What we need to produce the animation is just a script (no movie clip at all).
Preview
Source Code
[as3]//By : Isusx’s Programming Corner
//URL : http://isusx.com/
//Bouncing area setup
bt = 0;
bl = 0;
bb = 400;
br = 550;
//Variable declaration
this._v = Array();
this._o = Array();
//Lines colour setup
this._c = Array(
0xFF0000,
0×00FF00,
0×0000FF,
0×0FF000,
0×000FF0,
0×0F000F,
0xF0F000,
0×00F0F0,
0×000F0F,
0xF00F00,
0xF000F0,
0xF0000F
);
//Coordinate initialization
k10 = 2*br/3;
k11 = 0;
k20 = 0;
k21 = bb/3;
k30 = br/3;
k31 = bb;
k40 = br;
k41 = 2*bb/3;
//Number of polygons
rect = 12;
//Polygon initialize
function bouncing_line_init(ind, init_pos){
this._v[ind] = Array();
this._o[ind] = Array();
//Speed initialize
this._v[ind][0] = Array(4, 4);
this._v[ind][1] = Array(4, 4);
this._v[ind][2] = Array(-4, -4);
this._v[ind][3] = Array(-4, -4);
for(i=0;i<4;i++)
this._o[ind][i] = Array(init_pos[i][0], init_pos[i][1]);
}
//Polygon animate
function bouncing_line_animate(ind){
//Next coordinate calculation
for(i=0;i<4;i++){
this._o[ind][i][0] += this._v[ind][i][0];
this._o[ind][i][1] += this._v[ind][i][1];
}
//Draw the polygon
this.lineStyle(1,this._c[ind],100);
this.moveTo(this._o[ind][0][0], this._o[ind][0][1]);
for(i=1;i<4;i++)
this.lineTo(this._o[ind][i][0], this._o[ind][i][1]);
this.lineTo(this._o[ind][0][0], this._o[ind][0][1]);
//Speed determination
for(i=0;i<4;i++){
if(this._o[ind][i][0] < bl || this._o[ind][i][0] > br)
this._v[ind][i][0] = -this._v[ind][i][0];
if(this._o[ind][i][1] < bt || this._o[ind][i][1] > bb)
this._v[ind][i][1] = -this._v[ind][i][1];
}
}
//Stage setting
Stage.scaleMode = "noScale";
Stage.align = "TL";
//Polygons initialization
for(j=0;j<rect;j++){
spc = j*20;
bouncing_line_init(j, Array(
Array(k10-spc,k11),
Array(k20,k21+spc),
Array(k30+spc,k31),
Array(k40,k41-spc)
), this._c[j]);
}
//Animate process
this.onEnterFrame = function(){
this.clear();
for(j=0;j<rect;j++)
bouncing_line_animate(j);
}
[/as3]
Download
Click here to download the source code
Copyright Note
This script is free to use and can be modified as you wish.
Related Links
Flash Documentation, Guide and Complete Reference





