littlepad blog

サイン波の描画

おぉ!チープ!
でもスクリプトで曲線が書けたってことだけで小さな感動。

[swf]/blog/wp-content/uploads/2008/10/SinLine.swf, 550, 400[/swf]

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class SinLine extends Sprite
    {
        private var _angle:Number = 0;
        private var _rangeY:Number = 70;
        private var _speedX:Number = 8;
        private var _speedY:Number = 0.2;
        private var _posX:Number = 0;
        private var _posY:Number = 0;
        
        public function SinLine() {
            init();
        }
        
        private function init():void {
            graphics.lineStyle(1, 0, 1);
            graphics.moveTo(0, stage.stageHeight / 2);
            addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
        }
        
        private function onEnterFrame(e:Event):void 
        {
            _angle += _speedY;
            _posX += _speedX;
            _posY = stage.stageHeight / 2 + Math.sin(_angle) * _rangeY;
            graphics.lineTo(_posX, _posY);
            if (_posX > stage.stageWidth) {
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
        }
    }
}
モバイルバージョンを終了