React

React
Fundamental of React Js

Wednesday, January 2, 2008

Runtime Circle

Creating a method that can be used by all movieclips in a movie



As of Flash MX, the prototype property of the MovieClip class can be used to assign additional functions to the class, which makes them available to all movieclips. Here's an example of creating and using a draw circle method:

// r = radius of circle
// x, y = center of circle
MovieClip.prototype.drawCircle = function (r, x, y) {
var TO_RADIANS:Number = Math.PI/180;
// begin circle at 0, 0 (its registration point) -- move it when done
this.moveTo(0, 0);
this.lineTo(r, 0);

// draw 12 30-degree segments
// (could do more efficiently with 8 45-degree segments)
var a:Number = 0.268; // tan(15)
for (var i=0; i < endx =" r*Math.cos((i+1)*30*TO_RADIANS);" endy =" r*Math.sin((i+1)*30*TO_RADIANS);" ax =" endx+r*a*Math.cos(((i+1)*30-90)*TO_RADIANS);" ay =" endy+r*a*Math.sin(((i+1)*30-90)*TO_RADIANS);" _x =" x;" _y =" y;" x="100," y="100">
To draw a shape with a part of it cut out, like a donut eg, simply put all the commands to draw both the initial object (big circle) and the cutout (smaller circle) between the beginFill and endFill statements. Here's how the donut on the left (ok, more like a bagel but still delicious) was created:

// r1 = radius of outer circle
// r2 = radius of inner circle (cutout)
// x, y = center of donut
// This creates a donut shape (circle with a cutout circle)
MovieClip.prototype.drawDonut1 = function (r1, r2, x, y) {
var TO_RADIANS:Number = Math.PI/180;
this.moveTo(0, 0);
this.lineTo(r1, 0);

// draw the 30-degree segments
var a:Number = 0.268; // tan(15)
for (var i=0; i < endx =" r1*Math.cos((i+1)*30*TO_RADIANS);" endy =" r1*Math.sin((i+1)*30*TO_RADIANS);" ax =" endx+r1*a*Math.cos(((i+1)*30-90)*TO_RADIANS);" ay =" endy+r1*a*Math.sin(((i+1)*30-90)*TO_RADIANS);" i="0;" endx =" r2*Math.cos((i+1)*30*TO_RADIANS);" endy =" r2*Math.sin((i+1)*30*TO_RADIANS);" ax =" endx+r2*a*Math.cos(((i+1)*30-90)*TO_RADIANS);" ay =" endy+r2*a*Math.sin(((i+1)*30-90)*TO_RADIANS);" _x =" x;" _y =" y;" array =" [0," array =" [100," array =" [0," object =" {a:300,">

Using a shape with a cutout as a mask

If a shape containing a cutout is to be used as a mask, as the donut on the right above, care must be taken to draw the cutout in the opposite direction from that in which the original shape was drawn. (If you don't, there will be no cutout in the mask). Here's the code for the donut mask on the right, in which a big circle is drawn in a clockwise direction and a smaller circle drawn in the opposite direction (all before the endFill is applied). pic is a movieclip containing the flower graphic.

// r1 = radius of outer circle
// r2 = radius of inner circle (cutout)
// x, y = center of donut
// This creates a donut shape that can be used as a mask
MovieClip.prototype.drawDonut2 = function (r1, r2, x, y) {
var TO_RADIANS:Number = Math.PI/180;
this.moveTo(0, 0);
this.lineTo(r1, 0);

// draw the 30-degree segments
var a:Number = 0.268; // tan(15)
for (var i=0; i < endx =" r1*Math.cos((i+1)*30*TO_RADIANS);" endy =" r1*Math.sin((i+1)*30*TO_RADIANS);" ax =" endx+r1*a*Math.cos(((i+1)*30-90)*TO_RADIANS);" ay =" endy+r1*a*Math.sin(((i+1)*30-90)*TO_RADIANS);" i="12;"> 0; i--) {
var endx = r2*Math.cos((i-1)*30*TO_RADIANS);
var endy = r2*Math.sin((i-1)*30*TO_RADIANS);
var ax = endx+r2*(0-a)*Math.cos(((i-1)*30-90)*TO_RADIANS);
var ay = endy+r2*(0-a)*Math.sin(((i-1)*30-90)*TO_RADIANS);
this.curveTo(ax, ay, endx, endy);
}

this._x = x;
this._y = y;
}
createEmptyMovieClip("d2", 2);
d2.beginFill(0xaa0000, 60);
d2.drawDonut2(86, 36, 300, 94);
d2.endFill();
pic.setMask(d2);


courtesy : Flash-Creation

0 comments: