forked from: draw Star

by bkzen forked from draw Star (diff: 64)
draw star
* 星の描画
* もっと美しくてシンプルな描画方法をないものか…(>_<。) 
* 
♥0 | Line 45 | Modified 2010-08-05 11:56:00 | MIT License
play

ActionScript3 source code

/**
 * Copyright bkzen ( http://wonderfl.net/user/bkzen )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/t6ry
 */

// forked from takishiki's draw Star
/*
 * draw star
 * 星の描画
 * もっと美しくてシンプルな描画方法をないものか…(>_<。) 
 * 
 */
package
{
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.display.Sprite;
    
    [SWF (backgroundColor = "0xFFFFFF", frameRate = "30", width = "465", height = "465")]
    public class FlashTest extends Sprite
    {
        private var shape:Shape;
        
        public function FlashTest()
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e: Event = null): void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //
            addChild(shape = new Shape());
            shape.x = (stage.stageWidth - shape.width) / 2;
            shape.y = (stage.stageHeight - shape.height) / 2;
            shape.graphics.beginFill(0xFFFF00);
            shape.graphics.lineStyle(2, 0x999900);
            drawStar(shape.graphics, 100);
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        private function loop(e:Event):void 
        {
            shape.rotation+=4;
        }
        
        /**
         * 星を書く
         * @param    g    : 描画する対象のグラフィック
         * @param    r1    : 山の半径
         * @param    r2    : 谷の半径
         * @param    num    : 山の数
         * @param    x    : 中心x座標
         * @param    y    : 中心y座標
         * @param    deg    : 回転角度
         */
        private function drawStar(g: Graphics, r1: int, r2: int = 0, num: int = 5, x: int = 0, y: int = 0, deg: Number = 0): void
        {
            r2 ||= r1 >> 1;
            var i: int, rad: Number = (deg % 360) / 180 * Math.PI, d: Number = Math.PI / num;
            g.moveTo(x + r1 * Math.cos(rad), y + r1 * Math.sin(rad));
            for (i = 0; i < num; i++) 
            {
                rad += d;
                g.lineTo(x + r2 * Math.cos(rad), y + r2 * Math.sin(rad));
                rad += d;
                g.lineTo(x + r1 * Math.cos(rad), y + r1 * Math.sin(rad));
            }
        }
    }
}