[Free script] Colorful bouncing rectangles with ActionScript

Started by Xhanch Studio, April 14, 2011, 02:31:18 PM

previous topic - next topic
Go Down

Xhanch Studio

April 14, 2011, 02:31:18 PM Last Edit: April 14, 2011, 03:11:18 PM by Xhanch Studio
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

Code Select
//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][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);
}



Download

http://xhanch.com/gallery/action-script/colorful-bouncing-rectangles.zip
Best Regards,
Susanto B.Sc
----------------------------------------------------------------------------
Web development services, WordPress plugin and theme development, PSD to XHTML conversion - http://xhanch.com
Read free manga online - http://authrone.com

Go Up