flash on 2010-7-10

by kihon
♥0 | Line 48 | Modified 2010-07-10 06:50:24 | MIT License
play

ActionScript3 source code

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

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class Main extends Sprite
    {
        private var effect:SoundEffect;
        
        public function Main()
        {
            effect = new SoundEffect(10, 4);
            effect.x = effect.y = 100;
            addChild(effect);
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        // 毎フレーム更新
        private function onEnterFrame(event:Event):void 
        {
            effect.update();
        }
    }
}

import flash.display.Sprite;

class SoundEffect extends Sprite
{
    private var w_num:int;        // 横の数
    private var h_num:int;        // 縦の数(最大値)
    private var frame:int = 0;    // 速度調整用
    
    // コンストラクタで縦横の数を受け取る
    public function SoundEffect(w_num:int, h_num:int)
    {
        this.w_num = w_num;
        this.h_num = h_num;
    }

    public function update():void
    {
        // 速度調整。お好みで。
        if (frame++ % 2) return;
        
        // 配列に0~h_numまでのランダム値を入れておく
        var data:Array = [];
        for (var i:int = 0; i < w_num; i++) data[i] = int(Math.random() * h_num);
        
        // w_num * h_num分だけ長方形を用意
        graphics.clear();
        graphics.beginFill(0x888888);
        for (var x:int = 0; x < w_num; x++)
        {
            for (var y:int = 0; y < data[x]; y++)
            {
                graphics.drawRect(x * 5, y * -3, 4, 2);
            }
        }
        graphics.endFill();
    }
}