In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Math.random() で 0 は出るのか? (1)

Get Adobe Flash player
by ProjectNya 27 Apr 2011

    Talk

    ProjectNya at 27 Apr 2011 14:58
    出ないね。
    Embed
/**
 * Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7Yee
 */

////////////////////////////////////////////////////////////////////////////////
// Math.random() で 0 は出るのか? (1)
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    [SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]

    public class Main extends Sprite {
        private var label:Label;
        private var timer:Timer;

        public function Main() {
            //Wonderfl.capture_delay(1);
            init();
        }

        private function init():void {
            var wondflcolor:WonderflColor = new WonderflColor(465, 465);
            addChild(wondflcolor);
            //
            label = new Label(200, 20, 20, Label.CENTER);
            addChild(label);
            label.x = 132;
            label.y = 212;
            label.textColor = 0xFFFFFF;
            label.text = "計算中...";
            //
            timer = new Timer(1);
            timer.addEventListener(TimerEvent.TIMER, update, false, 0, true);
            timer.start();
        }
        private function update(evt:TimerEvent):void {
            var count:uint = evt.target.currentCount;
            var n:Number = Math.random();
            label.text = count + " 回目\n(" + n + ")";
            if (n == 0) {
                timer.stop();
                timer.removeEventListener(TimerEvent.TIMER, update);
                label.text = count + " 回目で 0 が出た!";
            }
        }
        
    }

}


//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

class Label extends Sprite {
    private var txt:TextField;
    private static var fontType:String = "_ゴシック";
    private var _width:uint = 20;
    private var _height:uint = 20;
    private var size:uint = 12;
    public static const LEFT:String = TextFormatAlign.LEFT;
    public static const CENTER:String = TextFormatAlign.CENTER;
    public static const RIGHT:String = TextFormatAlign.RIGHT;

    public function Label(w:uint, h:uint, s:uint = 12, align:String = LEFT) {
        _width = w;
        _height = h;
        size = s;
        draw(align);
    }

    private function draw(align:String):void {
        txt = new TextField();
        addChild(txt);
        txt.width = _width;
        txt.height = _height;
        txt.autoSize = align;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.embedFonts = true;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = size;
        tf.align = align;
        txt.defaultTextFormat = tf;
        textColor = 0x000000;
    }
    public function set text(param:String):void {
        txt.text = param;
    }
    public function set textColor(param:uint):void {
        txt.textColor = param;
    }

}


//////////////////////////////////////////////////
// WonderflColorクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;

class WonderflColor extends Sprite {
    private var color1:uint = 0x00AAE4;
    private var color2:uint = 0x0069A0;

    public function WonderflColor(w:uint, h:uint) {
        draw(w, h);
    }

    private function draw(w:uint, h:uint):void {
        var colors:Array = [color1, color2];
        var alphas:Array = [1, 1];
        var ratios:Array = [0, 255];
        var matrix:Matrix = new Matrix();
        matrix.createGradientBox(w*1.5, h*1.5, 0, -w*0.25, -h*0.25);
        graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
        graphics.drawRect(0, 0, w, h);
        graphics.endFill();
    }

}