Home > Action Script > Walled Bouncing Polygon Animation Using Flash And Action Script

Walled Bouncing Polygon Animation Using Flash And Action Script

Do you want to create an animation of bouncing rectangle using Flash and Action Script. In this animation, a polygon will keep bouncing after an edge hit a wall. The area is a rectangle so that the polygon 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/

//Speed initialization for 4 corners of polygon
v1 = Array(4, 4);
v2 = Array(4, 4);
v3 = Array(-4, -4);
v4 = Array(-4, -4);

//Coordinate initialization for 4 corners of polygon
a1 = Array(2*Stage.width/3, 0);
a2 = Array(0, Stage.height/3);
a3 = Array(Stage.width/3, Stage.height);
a4 = Array(Stage.width, 2*Stage.height/3);

this.onEnterFrame = function(){
//Bouncing Area
bt = 0;
bl = 0;
bb = Stage.height;
br = Stage.width;

//Calculation for new coordinate for 4 corners
a1[0] += v1[0];
a1[1] += v1[1];
a2[0] += v2[0];
a2[1] += v2[1];
a3[0] += v3[0];
a3[1] += v3[1];
a4[0] += v4[0];
a4[1] += v4[1];

//Draw the polygon
this.clear();
this.lineStyle(1,0,100);
this.moveTo(a1[0], a1[1]);
this.lineTo(a2[0], a2[1]);
this.lineTo(a3[0], a3[1]);
this.lineTo(a4[0], a4[1]);
this.lineTo(a1[0], a1[1]);

//Speed determination
if(a1[0] < bl || a1[0] > br)
v1[0] = -v1[0];
if(a1[1] < bt || a1[1] > bb)
v1[1] = -v1[1];

if(a2[0] < bl || a2[0] > br)
v2[0] = -v2[0];
if(a2[1] < bt || a2[1] > bb)
v2[1] = -v2[1];

if(a3[0] < bl || a3[0] > br)
v3[0] = -v3[0];
if(a3[1] < bt || a3[1] > bb)
v3[1] = -v3[1];

if(a4[0] < bl || a4[0] > br)
v4[0] = -v4[0];
if(a4[1] < bt || a4[1] > bb)
v4[1] = -v4[1];
}
[/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

If you like this code/script, perhaps you would kindly treat me a drink. Any number is welcome. Thanks :)

Related Article

  1. No comments yet.
  1. No trackbacks yet.