XHanch Studio Log in | Register | Cart

Forum

Notifications
Clear all

[Free script] Colorful bouncing rectangles with ActionScript

1 Posts
1 Users
0 Likes
1,756 Views
XHanch
(@xhanch-alt)
Posts: 2105
Member Admin
Topic starter
 

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 hits 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).

Demo preview

http://xhanch.com/gallery/action-script/colorful-bouncing-rectangles.swf

Source Code

//By    : Xhanch Studio
//URL  : http://xhanch.com/action-script-colorful-bouncing-rectangles/

//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,
    0x00FF00,
    0x0000FF,
    0x0FF000,
    0x000FF0,
    0x0F000F,
    0xF0F000,
    0x00F0F0,
    0x000F0F,
    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] = Array(init_pos[0], init_pos[1]);
}

//Polygon animate
function bouncing_line_animate(ind){
    //Next coordinate calculation
    for(i=0;i<4;i++){
        this._o[ind][0] += this._v[ind][0];
        this._o[ind][1] += this._v[ind][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][0], this._o[ind][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][0] < bl || this._o[ind][0] > br)
            this._v[ind][0] = -this._v[ind][0];
        if(this._o[ind][1] < bt || this._o[ind][1] > bb)
            this._v[ind][1] = -this._v[ind][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);
}

Download

http://xhanch.com/gallery/action-script/colorful-bouncing-rectangles.zip

 
Posted : 14/04/2011 8:31 pm
Share: