Macromedia Flash MX と Flash Lite の ActionScript について
Top back next

基本的な図形の直接制御1



Flash MXでは新しいコマンドを使って基本的な図形の頂点を直接制御できるようになりました。
beginFill, beginGradientFill, clear, curveTo, endFill, lineStyle, lineTo, moveTo

しかし、円とか長方形は、ユーザ関数を作る必要があります。
以下の元ソースは、Nuts & Bolts.DrawingToolsによるものです。

円の作成関数
MovieClip.prototype.drawCircle = function(r, x, y) {
    this.moveTo(x+r, y);
    a = Math.tan(22.5*Math.PI/180);
    for (var angle = 45; angle<=360; angle += 45) {
        // endpoint:
        var endx = r*Math.cos(angle*Math.PI/180);
        var endy = r*Math.sin(angle*Math.PI/180);
        // control:
        // (angle-90 is used to give the correct sign)
        var cx = endx+r*a*Math.cos((angle-90)*Math.PI/180);
        var cy = endy+r*a*Math.sin((angle-90)*Math.PI/180);
        this.curveTo(cx+x, cy+y, endx+x, endy+y);
    }
};
これの使用例は、次となります。
var c40 = this.createEmptyMovieClip("c", 1);
with (c40) {
   lineStyle(3);
   beginFill(0xff0000);
   drawCircle(40, 100, 100);
   endFill();
}

円



長方形の作成関数
MovieClip.prototype.rectangle = function(w, h, x, y, stroke, fill) {	
	this.lineStyle(stroke.width, stroke.color, stroke.alpha);
	this.beginFill(fill.color, fill.alpha);
	this.moveTo(0, 0);
	this.lineTo(w, 0);
	this.lineTo(w, h);
	this.lineTo(0, h);
	this.endFill();
	this._x = x;
	this._y = y;
}
使用例は、二種類の長方形を作成して、回転させています。(Download)
var c = this.createEmptyMovieClip("c", 1);	
var c_stroke = {width:1, color:0x0000aa, alpha:50};	
var c_color = {color:0xffff00, alpha:50};	
c.rectangle(80, 40, 50, 50, c_stroke, c_color);
//
var c1 = this.createEmptyMovieClip("clip", 2);
var c1_stroke = {width:1, color:0x00aa00, alpha:50};
var c1_color = {color:0x00ffff, alpha:50};
c1.rectangle(80, 40, 100, 100, c1_stroke, c1_color);
//回転させる
this.onEnterFrame = function() {
    c1._rotation += 3;
    c._rotation += 5;
}